Write a. txt file in Java to implement several methods of line feed:
1. Use the Escape Character "\ r \ n" in Java ":
Java code
- String STR = "AAA ";
- STR + = "\ r \ n ";
In this way, there will be line breaks behind Str.
Note: The order of \ r and \ n cannot be switched; otherwise, line breaks cannot be implemented.
2. bufferedwriter's newline () method:
Java code
- 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:
Java code
- String STR = "AAA" + system. getproperty ("line. separator ");
Postscript:
I am very grateful to hanz188. As far as I know, there should be no common line breaks in Windows and Linux/UNIX platforms. For common systems, you can use the following escape characters for line breaks:
Text File line break in Windows: \ r \ n
Text File line break in Linux/Unix: \ r
Text File line break in MAC: \ n