Use BufferedReader to read line by line

InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);

String line ;
while ((line = br.readLine()) != null){
System.out.println(line);
}

Earlier i was doing one mistake

invoking readLine() both for checking null and for printing so as a effect the values were skipped and alternate values were being printed. This is due to fact that readLine was called two times and one value was being skipped

Useful resource

http://www.java2s.com/Code/Java/File-Input-Output/UseBufferedReadertoreadlinebyline.htm

No comments:

Post a Comment

Please share your views and comments below.

Thank You.