Java output line-wrapping standard posture "Line.separator"

Source: Internet
Author: User

Write a. txt file in Java to implement several methods of line wrapping:
1. Use the escape character "\ r \ n" in Java:
Text file line break under Windows: \ r \ n
Text file line break under Linux/unix: \ r
Text file line break under Mac: \ n

1.String str= "AAA";   2.str+= "\ r \ n";  

2.BufferedWriter newline () method:

FileOutputStream fos=New fileoutputstream ("C;\\11.txt");  BufferedWriter BW=new  bufferedwriter (FOS);  Bw.write ("Hello");  Bw.newline ();  Bw.write ("Java");  Bw.newline ();   

     /*** Creates a new buffered Character-output stream that uses an output * buffer of the given size. *     * @paramOut A Writer *@paramsz Output-buffer size, a positive integer * *@exceptionillegalargumentexception If sz is <= 0*/     PublicBufferedWriter (Writer out,intSZ) {    Super(out); if(SZ <= 0)        Throw NewIllegalArgumentException ("Buffer size <= 0");  This. Out =Out ; CB=New Char[SZ]; Nchars=sz; Nextchar= 0; LineSeparator=(String) java.security.AccessController.doPrivileged (NewSun.security.action.GetPropertyAction ("Line.separator")); }
BufferedWriter in NewLine () method:/*** Writes a line separator. The line separator string was defined by the * system property <tt>line.separator</tt>, and was not Necessar ily a single * newline (' \ n ') character. * * @exceptionIOException If An I/O error occurs*/ Public voidNewLine ()throwsIOException {write (lineseparator); }

3. Use the System.getproperty () method:

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

Http://www.cnblogs.com/todoit/archive/2012/04/27/2473232.html

PrintWriter in the following with PW instead, in writing client and server to test the communication program, with PW.PRINTLN (str) can send data to the client, and Pw.write (str) is not!
View Source Discovery:
The Pw.println (str) method is composed of the Write method and the println () method, and the newline () method is executed in the println () method.
and NewLine () in the implementation of a out.write (LineSeparator);
That is, the println (str) method outputs a lineseparator character more than the Write method;
Where LineSeparator is implemented as:
LineSeparator = (String) java.security.AccessController.doPrivileged (New Sun.security.action.GetPropertyAction (" Line.separator "));
The Line.separator attribute is not the same as it is for each system.
The notes in the println () method refer to:
/**
* Terminates the writing the line separator string. The
* Line separator string was defined by the system property
* &LT;CODE&GT;LINE.SEPARATOR&LT;/CODE&GT; necessarily a single newline
* Character (<code> ' \ n ' </code>).
*/
On my machine (Windows) test, the default LineSeparator output hex is 13 10 that is \ r \ n
The Write method is modified to: Write (str+ "\ r \ n") achieves the same effect as println (str).

http://blog.csdn.net/vhomes/article/details/6650576

Java output line-wrapping standard posture "Line.separator"

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.