1024programmer Java Analyze \r and \n from java, windows, linux

Analyze \r and \n from java, windows, linux

First give the ASCII values ​​of \r and \n

In Windows, \r\n means line break:

Create a new txt file, enter A and line feed B, and then check the hexadecimal value of the txt. Refer to the picture above. 0D 0A means carriage return and line feed (\r\n). The result is as follows:

In Linux, \n means line break:

Also create a new txt file in Linux, enter A and line break B, and then check the hexadecimal value of the txt. There is only 0A, which is line break (\n). The result is as follows:


The following is opened with Notepad. You can see that there is no line break.

Question 1: Why do some files only have \n? Why can I also see line breaks?

Because some editing tools have done special processing, if there is only \n, it will be treated as a line break.

You can see that after opening the file created in Linux above in Notepad, a miracle happened and there was no line break. But when I changed the editor to view it, a miracle happened again, and I could see line breaks again.

Sometimes when I take the log log under Linux to Windows and open it with Notepad, there is no line break. When I open it with other tools, I see that there is a line break. This is the reason.

In JAVA, for line breaks, you can refer to the API, as shown below

In fact, \n is often used in Java to indicate line breaks. Why not use \r\n, because using \n can achieve the line break effect and save bytes. But it’s okay to use \r\n.

But some people say that only \r can achieve the line breaking effect, which is misleading, please test

System.out.println(“diu le you mu you\r!!!\r”);
System.out.println(“rnrn\r\nrnrn”);
System.out. println(“nn\nnn”);

You can indeed see the line break effect in Eclipse. Please note that this is an optimization done by eclipse, just like the editing tools mentioned above

This is the result of executing the cmd command directly

This is the result of execution in IDEA, and there are more mistakes

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/analyze-r-and-n-from-java-windows-linux/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索