ReadLine () method in Java Why do some lines not read?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.