Today, when using Java for IO operations, the readLine () output to the console is a lot less. ReadLine () was later found to actually read one line at a time. If we don't talk. ReadLine () reads the contents of the line to a string, each call to the ReadLine () method, is actually read a line down. Finally you will find it turned into interlaced reading.
In the following two paragraphs of code. Direct System.out.println (Br.readline ()) is actually the next line after the output while loop. When we assign the result of ReadLine () to a variable, the output is the contents of the variable without calling the ReadLine () method again, so the output is normal content.
The contents of some lines of the output file:
1 Public Static voidMain (string[] args)throwsException {2 //get read stream3FileReader reader =NewFileReader ("c:\\users\\ Yang Huabin \\Desktop\\test.txt");4BufferedReader br =NewBufferedReader (reader);5 6 while(Br.readline ()! =NULL) {7 //Note that the output is ReadLine (), and the ReadLine () method in the and output of the while loop is dropped two times, so it will be read interlaced. 8 System.out.println (Br.readline ());9 }Ten One //turn off Read stream A br.close (); - reader.close (); -}
Normal output text amount content:
1 Public Static voidMain (string[] args)throwsException {2 //get read stream3FileReader reader =NewFileReader ("c:\\users\\ Yang Huabin \\Desktop\\test.txt");4BufferedReader br =NewBufferedReader (reader);5 6String string =NULL;7 while(String = Br.readline ())! =NULL) {8 //Note that the output is a string, and the ReadLine () method is actually called only once. 9 System.out.println (string);Ten } One A //turn off Read stream - br.close (); - reader.close (); the}
ReadLine () method in Java Why do some lines not read?