About line breaks in Java

Source: Internet
Author: User

There are 3 ways to implement line wrapping in Java:

1. Use the escape character "\ r \ n" in Java:

String str= "AAA";

str+= "\ r \ n";

This will be followed by a newline after Str.

Note: The order of \r,\n cannot be swapped, otherwise the effect of line wrapping cannot be achieved.

2.BufferedWriter newline () method:

FileOutputStream fos=new FileOutputStream ("C;\\11.txt");

BufferedWriter bw=new bufferedwriter (FOS);

Bw.write ("Hello");

Bw.newline ();

Bw.write ("Java");

W.newline ();

3. Use the System.getproperty () method: •

String str = "AAA" +system.getproperty ("Line.separator");

Different operating systems, line Fu Cao are also different:

\r:return to the leftmost side of the current line.

\n:newline moves down one line and does not move around.

Linux indicates: carriage return + newline;

In Windows: Carriage return + line break.

Mac means: Carriage return + line break.

History:

The origins and differences between the two concepts of carriage return "(carriage return) and" line feed ". Before the computer appeared, there was a thing called a telex typewriter (the concept of TTY under the teletype Model 33,linux/unix), which can be played 10 characters per second. But it has a problem, that is, when the line is finished, it will take 0.2 seconds to hit two characters. If there are new characters coming in this 0.2 seconds, then this character will be lost. So, the developers think of a way to solve this problem, is to add two after each line to end the character. One is called "return", which tells the typewriter to position the printhead at the left border, and the other is called "newline", which tells the typewriter to move the paper down one line. This is the origin of "line break" and "carriage return", which can be seen from their English name one or two

About line breaks in Java

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.