Reading lines with BufferedReader and checking for end of file
Solution 1
Am... You can simply use such construction:
String line;
while ((line = r.readLine()) != null) {
// do your stuff...
}
Solution 2
If you want loop through all lines use that:
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
Solution 3
You can use the following to check for the end of file.
public bool isEOF(BufferedReader br)
{
boolean result;
try
{
result = br.ready();
}
catch (IOException e)
{
System.err.println(e);
}
return result;
}
Solution 4
In your case you can read the next line because there may be something there.If there isn't anything, your code won't crash.
String line = r.readLine();
while(line!=null){
System.out.println(line);
line = r.readLine();
}
Solution 5
A question in the first place, why don't you use "Functional Programming Approach"? Anyways, A new method lines() has been added since Java 1.8, it lets BufferedReader returns content as Stream. It gets all the lines from the file as a stream, then you can sort the string based on your logic and then collect the same in a list/set and write to the output file. If you use the same approach, there is no need to get worried about NullPointerException. Below is the code snippet for the same:-
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
public class LineOperation {
public static void main(String[] args) throws IOException {
Files.newBufferedReader(Paths.get("C://xyz.txt")).
lines().
collect(Collectors.toSet()). // You can also use list or any other Collection
forEach(System.out::println);
}
}
Zippy
Oracle Certified Associate, Java and Android developer. All-round lover of all things tech like my cutting edge ZX-81!
Updated on April 29, 2022Comments
-
Zippy about 2 years
If I have something like this in my code:
String line = r.readLine(); //Where r is a bufferedReader
How can I avoid a crash if the next line is the end of the file? (i.e. null)
I need to read the next line because there may be something there that I need to deal with but if there isn't the code just crashes.
If there is something there then all is OK, but I can't be guaranteed that there will be something there.
So if I do something like: (pseudo code):
if (r.readLine is null) //End code else {check line again and excecute code depending on what the next line is}
The issue I have with something like this is, that when I check the line against null, it already moves onto the next line, so how can I check it again?
I've not worked out a way to do this - any suggestions would be a great help.
-
Zippy about 9 yearsReally wish people would leave a reason when down-voting. A down-vote is supposed to indicate there is something wrong with a question so maybe it can be reformatted. Down-voting without leaving a reason as to why the downvote has been cast helps no-one
-
-
Azad almost 11 yearsAs you answered first, I'll delete mine:)
-
Solomon Ucko over 6 yearsWould this actually throw
NullPointerException
? I think you need to checkif (s == null)
. -
miyalys over 5 yearsThis is better than assigning + comparing in the while clause, in my opinion.
-
user2665801 over 5 yearsThis fails silently on exception and BufferedReader::ready can return false even if the next read will succeed.
-
DarkHark almost 5 yearsKeeping the assignment out of the while clause keeps it much cleaner.
-
Florian F about 4 yearsThere is no reason to collect the lines. Remove the
collect()
call and doforEach()
on the stream.