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
* <CODE>LINE.SEPARATOR</CODE> 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"