When both the output and input streams are opened, the text content disappears.

Source: Internet
Author: User

When both the output and input streams are opened, the text content disappears.
When you open both the output and input streams for a text file, if you use the API accidentally, you will find that the output stream output is empty for the following reasons:

Public static void main (String [] args) {FileInputStream FD = null; FileOutputStream fos = null; try {File file = new File ("D: // test.txt "); FS = new FileInputStream (file);/*** new FileOutputStream (file) * new FileOutputStream (file, true) append mode * if no append mode is selected, the contents of the original file will be cleared, such as FileWriter (which is internally maintained by FileOutputStream... */Fos = new FileOutputStream (file); byte [] B = new byte [1024]; int index = 0; while (index = FCM. read (B)> = 0) {System. out. println (new String (B, 0, index);} // "\ r \ n" is the line break fos. write ("\ r \ ntest ". getBytes ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (FS! = Null & fos! = Null) {fos. close (); FCM. close () ;}} catch (IOException e) {e. printStackTrace ();}}}

 

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.