Java Fundamentals Hardening IO Flow Note 20:fileoutputstream Write data to implement line breaks and append writes

Source: Internet
Author: User

1. How do I make a line break for my data?

(1)

1  PackageCom.himi.fileoutputstream;2 3 Importjava.io.FileNotFoundException;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 7 8 /**9  * Ten * How to implement the data line wrapping?  One  * A  */ -  -  the  Public classFileOutputStreamDemo4 { -  -      Public Static voidMain (string[] args)throwsIOException { -         //creating a byte output stream object +FileOutputStream fos =NewFileOutputStream ("Fos3.txt"); -         //Write Data +          for(inti=0; i<10; i++) { AFos.write ("Love" +i). GetBytes ()); at         } -              -         //Freeing Resources - fos.close (); -          -          in     } -  to}

The effect is as follows: We don't have any new lines here, it's not what we want.

There is no line break here because you write the data without writing a newline character and writing a newline character.

(2) How do I write a newline character? As follows:

1 ImportJava.io.FileOutputStream;2 Importjava.io.IOException;3 4 5 /**6  * 7 * How to implement the data line wrapping? 8 * There is no line break because when you write to the data, there is no line break, write a newline character. 9  *Ten * line breaks for different systems are not the same: One * Windows:\ r \ n  A * Linux:\ n  - * Mac:\ r  - * While the usual advanced Notepad software is able to recognize any line break (such as the eclipse comes with Notepad software) the  */ -  -  -  Public classFileOutputStreamDemo4 { +  -      Public Static voidMain (string[] args)throwsIOException { +         //creating a byte output stream object AFileOutputStream fos =NewFileOutputStream ("Fos3.txt"); at         //Write Data -          for(inti=0; i<10; i++) { -Fos.write ("Love" +i). GetBytes ()); -Fos.write ("\ n". GetBytes ()); -         } -              in         //Freeing Resources - fos.close (); to          +          -     } the  *}

The results are as follows:

Go back to the project directory, as follows:

Open with notepad++, as follows:

Use Windows to bring your own Notepad open, as follows:

(3) Modify the code as follows:

1  PackageCom.himi.fileoutputstream;2 3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 6 7 /**8  * 9 * How to implement the data line wrapping? Ten * There is no line break because when you write to the data, there is no line break, write a newline character.  One  * A * line breaks for different systems are not the same: - * windows:\r\n  - * linux:\n  the * mac:\r  - * While the usual advanced Notepad software is able to recognize any line break (such as the eclipse comes with Notepad software) -  */ -  +  -  Public classFileOutputStreamDemo4 { +  A      Public Static voidMain (string[] args)throwsIOException { at         //creating a byte output stream object -FileOutputStream fos =NewFileOutputStream ("Fos3.txt"); -         //Write Data -          for(inti=0; i<10; i++) { -Fos.write ("Love" +i). GetBytes ()); -Fos.write ("\ r \ n". GetBytes ()); in         } -              to         //Freeing Resources + fos.close (); -          the          *     } $ Panax Notoginseng}

After running it found that all of the above Notepad software is wrapped, including the windows comes with Notepad software, as follows:

2. How do I add data ?

Use construction method: FileOutputStream (String name, Boolean append)

The parameter append is True, indicating append, and false to no append

The code is as follows:

1  PackageCom.himi.fileoutputstream;2 3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 6 7 /**8 * How to implement data append write? 9 * Using construction Method: FileOutputStream (String name, Boolean append)Ten * The parameter append is True, indicating append, and false to not append One  */ A  -  -  Public classFileOutputStreamDemo5 { the  -      Public Static voidMain (string[] args)throwsIOException { -         //creating a byte output stream object -         //FileOutputStream fos = new FileOutputStream ("Fos3.txt"); +          -         //FileOutputStream (String name, Boolean append) +FileOutputStream fos =NewFileOutputStream ("Fos3.txt",true); A         //Write Data at          for(inti=0; i<10; i++) { -Fos.write ("Love" +i). GetBytes ()); -Fos.write ("\ r \ n". GetBytes ()); -         } -              -         //Freeing Resources in fos.close (); -          to          +     } -  the}

The results are as follows:

Java Fundamentals Hardening IO Flow Note 20:fileoutputstream Write data to implement line breaks and append writes

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.