This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Text file problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Text file problem" Watch "Text file problem" New topic
Author

Text file problem

Suleman zia
Ranch Hand

Joined: Apr 19, 2007
Posts: 70
Hello All. This is my first posting in Javaranch.

I was reading alot of posts over here and looks to me this forum is much better than java forums. people are polite and are willing to help a noob or advance person with their java query rather than busting them and giving google searches to do their own work.

I have been programming in java for 9 months. I really like it. The best OOP i have ever experience after C++. i hate pointers and data structures and i am glad with Java you dont need to know about data structures alot. where as in C++ most of the things are done with pointers. Anyway back to my question

I am having alot of trouble in file reading. like for example i have a file something like this :

Name Product price quantity
xyz Wheels $20.25 10
abc Jam $3.01 1

Now my question is how can i read each specific line in java. whats the strategy i had to use ? i know BufferedReader and string.split is used alot in file reading but i am really very confused. any hints or any example or any strategies in reading a text file and having the output also in a file or in a program will be great.

Secondly here is one more example
Name: Suleman Zia
Class: COSI455
Subject: Java
Grade: B

Name: Rahim
Class Abc
Subject: History
Grade: C

How can i read this text file and displaying the output for each specific person. once i can display then i can encapsulate my data and can call addition? The question remains the same. how can i read this text file data for each person? Hope you can understand now what i am trying to say.

One more question what is the difference between DataOutputStream and PrintOutputStream File file = new File("saloo.txt").

Too many questions. hope to get some help in understanding the file system.

Secondly what if i would like to store the data in a ArrayList or Set? i know how to do it in a regular program. Give me a program to write with HasSet or Map and i will make sure to complete that task easily may be not that easily but what if i would like to store all of the text data or may be the first row in an arraylist or vector then how can i do it?

Thanks for the help and i really appreciate it.
[ April 21, 2007: Message edited by: Suleman zia ]

SCJP 1.5
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Suleman,
Welcome to JavaRanch!

The BufferedReader class provides a readLine method to read in a file one line at a time. This is good if you want to do the processing as you read in a file. String.split is good if someone passes you a file as a String. There is a common pattern for reading in a file. See the getContents method in this example. You need to write the logic to store the appropriate lines in the appropriate fields of your object.

I can't find a class in the JavaDoc called PrintOutputStream.

> Secondly what if i would like to store the data in a ArrayList or Set? i know how to do it in a regular program.
The same way you would do so in a regular program. You create the objects based on the lines and then add them to the data structure.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Suleman zia
Ranch Hand

Joined: Apr 19, 2007
Posts: 70
Thanks Jeanne.

So basically i will be using String split method if i have a file something like this.

Suleman : 20 : Welcome

s.split(" : ");

and if i want to read the contents of the file then i will use the readLine method from the BufferedReader.

Let me go and look at that url the one which you posted.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39072
    
  23
An alternative to Jeanne Boyarsky's suggestion, whihc will only work in Java 5 or Java 6, is to use the Scanner class. It is described as "simple," but it is very effective. I think it actually takes a BufferedReader and uses that to get at the data. You can say
BTW: you may have problems with the delimieter you used for the String.split method. If you write " : " it will insiste on looking for space-colon-space.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39072
    
  23
I ought to have read your posting properly before going on about space-colon-space. Sorry. You will have to make sure that in the example you quite you always have " : " between successive entries.
Casey Buresh
Greenhorn

Joined: Oct 03, 2006
Posts: 18
I'm a noob here, but it seems to me that this is a very simple question. I have only been programming in java for a couple months. If you just want to display information about a particular person or what not, why not make a two dimensional array containing all the information that you can just call the specific information or use a binary search or something like that in order to call it for displaying. In my class thats what the group making the school compiler is doing.


'allo Poppet!!!
 
GeeCON Prague 2014
 
subject: Text file problem