File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Read lines from a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read lines from a text file" Watch "Read lines from a text file" New topic
Author

Read lines from a text file

Sergio Zuniga
Greenhorn

Joined: Nov 18, 2003
Posts: 12
Hi,
i'm new in java, and ai don't know how to read a line from a text file. the line has 2 info, the name of a person (20 char) an the rest of the line are letters from a to e, like this:
JON DOE AABBCCCD E DDBBCC
and the file has many lines, and i want to analize each line.
I was doing something like this, but it reads all the text.
Any suggestion?
thanks
--------------------------------------------------------------------------
try {
BufferedReader in = new BufferedReader(new FileReader("PAA.txt"));

String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
in.close();
}
catch (IOException e) {
}
}
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
Do whatever analyzing you need to do before or after your line with System.out.println(str) (or instead of, depending on whether you really want to print it to the screen).
If your analysis is complicated, I would suggest putting it into a different method and then calling that method from within your while loop.
Angel
[ February 10, 2004: Message edited by: Angel Dobbs-Sciortino ]
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
You're going in the right direction.
You'll want to do your analyzing when you have the line as a string, not as you read it in. String in memory = fast, many unneccessary disk reads = slow
Anyway, if you want to analyze it on the way in, use an InputStreamReader
hth
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your code looks real close. You'll want to move the close outside the while loop so it happens after everything has been read.
You mentioned it reads all the text ... do you mean a single readLine() gets the whole file? That may mean there are no newlines in there. Does the file display nicely lined up if you just type it? Or all jumbled into a long stream?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Stan is correct about the close. You need to move it outside of the while loop. As for reading the entire line you need to use the streamTokenizer class to organize the letters and recognize the delimeters (whitespace spaces).
if you want to organize it without just reading the entire line then use the following code: ( I just sort of wipped it up so if it doesn't work lemme know) but this code should get you started.
HTH
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Sergio, I think you are well on your way there. Just analyze each line as you read it in:

NOTE: String.split() method is available in sdk 1.4 and up only.
Jamie
Sergio Zuniga
Greenhorn

Joined: Nov 18, 2003
Posts: 12
Hey, thank you all!!
I put some ideas into practice and it works fine separating the name from the letters, but the problem that i have is that the space from de letter list is a valid entry too and i didn't know it!! until now , just like another letter, so it must be analized to. I'm looking a way to get the entire list including the white spaces such as " ABBBD EE E C DDD" (the white spaces are questions not answered by the person).
Thanks very much for all.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Read lines from a text file