Java write to. txt file, how to implement the line-wrapping __file

Source: Internet
Author: User

Java to write a. txt file, to implement several ways to wrap:
1. Use the escape character "\ r \ n" in Java:

Java code

1. String str= "AAA";

2. str+= "\ r \ n";


So there's a line of change behind Str.
Note: The order of \r,\n can not be exchanged, otherwise can not achieve the effect of line wrapping.


2.BufferedWriter newline () method:

Java code

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

2. BufferedWriter bw=new bufferedwriter (FOS);

3. Bw.write ("Hello");

4. Bw.newline ();

5. Bw.write ("Java");

6. W.newline ();



3. Use the System.getproperty () method:

Java code

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


As far as I know, there should be no common line breaks in Windows and Linux/unix platforms. For commonly used systems, you can use the following escape characters to implement line breaks:
Text file newline characters under Windows: \ r \ n
Text file newline characters under Linux/unix: \ r
Text file line feed under Mac: \ n

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.