This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes FileReader / BufferedReader will only read the first line from the file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "FileReader / BufferedReader will only read the first line from the file" Watch "FileReader / BufferedReader will only read the first line from the file" New topic
Author

FileReader / BufferedReader will only read the first line from the file

Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
Hi,

I have a problem with the following code, only the first line of text in the file is read and returned.

Code:

Output:


Entia non sunt multiplicanda praeter necessitatem
K. Tsang
Ranch Hand

Joined: Sep 13, 2007
Posts: 1219

The reason why you are only reading the first line is your while loop. You read the line in then what happens? Loop through it until i reaches 3? Try putting that line variable inside the while declaration. You will see a whole lot of lines read.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
Seems an odd while-loop; why not this?

. . . while (i < 3 && (line = myReader.nextLine()) != null) . . .

Where are you reading the line? You only appear to read one line once. You will have to read it again. Using conventional forms of loops will tend to reduce errors, because the conventional forms have been tried millions of times before and seen to work correctly.
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
duh... thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileReader / BufferedReader will only read the first line from the file
 
Similar Threads
Global Array not working
reading a user chosen file into array
reading a user chosen file into array - multiple files
Overhead IO : The cryptic case of the extra bytes
Calling the method from another method within the same class