• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help reading File  RSS feed

 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a .txt file with some sentences typed in in this format:
Sentence 1
Sentence 2
Sentence 3

Sentence 4
Sentence 5

I can read and show the contents of the file but what the problem is I just want it to show sentences 1 through 3. What do i use for it to recognize the new line? Ive tried the Search but to no avail.
-Clay
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you doing currently to read the file and display it? If you are only using a FileReader, it only allows you to read a single character at a time. However, you can use a BufferedReader to wrap around it and then read a whole line at a time. If you don't know how to do this (or even if you do), I strongly suggest you the I/O trail from the Java Tutorial. This should get you up to speed on the various classes that are available for reading and writing files.

Layne
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay heres my code:


What i want to do is when there is a blank line to have it stop reading. Ive tried if(line=="\n") but it doesnt work. Anyone know the correct way?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Display line.trim().length() of each line inside your loop and see if you can find a way for the program to spot the blank one.
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much. Works now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!