Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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

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
internet detective

Joined: May 26, 2003
Posts: 29249

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

Joined: Oct 13, 2005
Posts: 36508
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

Joined: Oct 13, 2005
Posts: 36508
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

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!!!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Text file problem
Similar Threads
Reading blocks of data from files
Interview code quiz preparation
I'm looking for JNI real world examples
XML Crazy
pointers in java