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