Question : Read file line by line in java

I am trying to read the file line by line and trying to display.unfortunately, I can display every 3rd line.

i.e:

One
Two
Three
Four
Five
Six
Seven
Eight
Nine

I am able to display only
One
Three
Five
Seven

Null Pointer Exception for the last Line

Am I missing anything, any ideas?

Thanks.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
FileInputStream fis = new FileInputStream(dir);
DataInputStream dataInputStream = new DataInputStream(fis);
InputStreamReader inputStream = new InputStreamReader(dataInputStream);
BufferedReader brreader = new BufferedReader(inputStream);

while ((line = brreader.readLine()) != null) {


System.out.println("---Line---" + line);


//Do something


}

if ((line = brreader.readLine()) == null) {


}

Answer : Read file line by line in java

<<I am able to display only
One
Three
Five
Seven>>

It is because you are calling the readline method twice in the loop, which will cause the next line to be fetched from file and that line will be skipped eventually. remove line number 17-20 and try again

Random Solutions  
 
programming4us programming4us