Java write file for line break

Source: Internet
Author: User

The first type:

Write content using \ r \ n for line wrapping

File File =NewFile ("D:/text"); Try {                if(!file.exists ()) file.createnewfile (); FileOutputStream out=NewFileOutputStream (file,false); StringBuffer SB=NewStringBuffer (); Sb.append ("10600257100120161201153103010 \ r \ n"); Sb.append ("120161201kbs571009886631 Zhejiang catalogue upload 120161201094425210009302359591120110422kbs00005595530zza571zza20161201094435fanzhipeng2000 \ n "); Out.write (Sb.tostring (). GetBytes ("Utf-8"));//Note that you need to convert the corresponding character setOut.flush ();                Out.close (); /*<br> FileOutputStream out = new FileOutputStream (file); <br>
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (Writerstream, "UTF-8")); <br>
Writer.write (JSON);<br>
writer.close ();
<em id= "__mceDel" >                                */<br>                                 catch (IOException e) {                  e.printStackTrace();              } </em>

The second type:

Using the newline () method of BufferedWriter

File File =NewFile ("D:/text"); Try {                if(!file.exists ()) file.createnewfile (); FileWriter out=NewFileWriter (file); BufferedWriter BW=NewBufferedWriter (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.

Java write file for line break

Related Article

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: 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.