I have written below code for reading a file:
package com.test.application;
import java.io.*;
import java.io.IOException;
public class FileRead {
public static void main(String[] args) {
try{
File file=new File("Hello.txt");
FileReader fileReader=new FileReader(file);
BufferedReader reader=new BufferedReader(fileReader);
/*String line=null;
while((line = reader.readLine())!=null){
System.out.println(line);
} */
System.out.println("This is using no string variable!!!");
while(reader.readLine()!=null){
System.out.println(reader.readLine());
}
reader.close();
}
catch(IOException e){
}
}
}
my text file is:
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting
industry.
Lorem Ipsum has been the industry's standard dummy text ever since the
1500s, when an unknown printer took a galley of type and scrambled it
to make a type specimen book.
It has survived not only five centuries, but also the leap into
electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets
containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem
Ipsum.
My question here is when i am using a String variable to read from file, i ma getting all contents of file, i.e:
String line=null;
while((line = reader.readLine())!=null){
System.out.println(line);
}
however when i used the below code snippet for reading through file, few lines where skipped, and whole of the file was not getting read.
while(reader.readLine()!=null){
System.out.println(reader.readLine());
}
Can anyone please explain why this is happening.