• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Read lines from a text file

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {
}
}
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic