The first type:
Write content using \ r \ n for line wrapping
File File = new file ("D:/text"); try {if (!file.exists ()) file.createnewfile (); FileOutputStream out=new FileOutputStream (file,false); StringBuffer sb=new StringBuffer () sb.append ("10600257100120161201153103010 \ r \ n"); Sb.append (" 120161201kbs571009886631 Zhejiang Catalogue upload 120161201094425210009302359591120110422kbs00005595530zza571zza20161201094435fanzhipeng2000 \ n "), Out.write (Sb.tostring (). GetBytes (" Utf-8 ")), or//note the need to convert the corresponding character set Out.flush (); Out.close ();/*
FileOutputStream out = new FileOutputStream (file);
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (Writerstream, "UTF-8"));
Writer.write (JSON);
Writer.close ();
*/
} catch (IOException e) {e.printstacktrace ();}
The second type:
Using the newline () method of BufferedWriter
File File = new file ("D:/text"); try {if (!file.exists ()) file.createnewfile (); FileWriter out=new FileWriter (file); BufferedWriter bw= New BufferedWriter (out); Bw.write ("10600257100120161201153103010"); Bw.newline (); Bw.write (" 120161201kbs571009886631 Zhejiang Catalogue upload 120161201094425210009302359591120110422kbs00005595530zza571zza20161201094435fanzhipeng2000 "); Bw.newline (); Bw.flush (); Bw.close ();} catch (IOException e) {e.printstacktrace ();}
However, there may be problems with newline in use:
Line breaks for different systems:
Windows--\ r \ n
Linux--\ R
Mac--\ n
Our general development is developed under Windows, and the server is typically Linux.
If we use the newline function to wrap, in the native test, because it is the Windows environment, the line break is \ r \ n, open the file when the natural file is a newline, no problem.
When we deploy to the server, the server is the Linux environment, the newline read system line break is \ r, exported to a file, the file line break is \ r, when we download this file through the browser to Windows, then open the file will be there is no line-wrapping problem. Because the explanation for \ r under Windows is not a line break.
Therefore, when developing, we cannot use the newline method if we need to specify that the file should be wrapped in some places. You must specify a newline character manually: \ r \ n Because, according to the different system line-breaks listed above, if the end of the string is \ r \ n in three systems, viewing the file will be interpreted as a newline.
Simple Finishing!!
Java write file for line break