ReadLine () and "\ r", "\ n" Issues

Source: Internet
Author: User

Many of the input streams have a function readline (), and we often use this function, but sometimes it can be a bit of a hassle if you don't think about it seriously.

If we are reading from the console, we may not have thought that the ReadLine function is based on "\ r", "\ n" which is to intercept the string, because the general computer implementation when the ENTER key is pressed after the corresponding "\ r", "\ n";

Add: "\ r" is to move the cursor to the beginning of a line, "\ n" is changed to the next line, different system processing method, the UNIX system "\ n" will move to the beginning of the next line, Windows is the surface meaning. The Mac's "\ R" goes back to the beginning and moves to the next line.

According to my test, the string returned by ReadLine does not contain the ending "\ r", "\ n".

Example:

String line ="hello\r"; OutputStream out=NewFileOutputStream (".//out.txt");  out. Write (Line.getbytes ()); InputStreaminch=NewFileInputStream (".//out.txt"); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (inch)); String Str=Reader.readline (); System. out. println ("ReadLine Read-out length:"+str.length () +"ReadLine Read the result:"+STR);

The result of the output is:

ReadLine read-out length: 5 readLine read result: Hello

As you can see, the ReadLine function automatically intercepts strings before "\ r", "\ n".

String line ="hello\r"; OutputStream out=NewFileOutputStream (".//out.txt");  out. Write (Line.getbytes ()); InputStreaminch=NewFileInputStream (".//out.txt"); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (inch)); String Str=Reader.readline (); /*System.out.println ("readLine read out Length:" +str.length () + "readLine Read result:" +str);*/                byte[] B =New byte[ -]; inch. Read (b,0, Line.length ());  for(inti =0; I<line.length (); i++) {System. out. println (Char) b[i]); } System. out. println ("end!");

Output Result:

H
E
L
L
O


end!

It is shown here that if read is used, there is no such case and it is read by byte.

ReadLine () and "\ r", "\ n" Issues

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.