Java Fundamentals Hardening IO Flow Note 22:fileinputstream/fileoutputstream Copy text file case

Source: Internet
Author: User

1. Use byte stream fileinputstream/fileoutputstream to copy a text file case:

Analysis:

(1) data source : Where to come from

A.txt--Read data--FileInputStream

(2) destination : Where to go

B.txt--Write data--FileOutputStream

2. code example:

1  Packagecn.itcast_03;2 3 ImportJava.io.FileInputStream;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 7 /*8 * Copy text files. 9  * Ten * Data Source: Where to come from One * a.txt--read data--FileInputStream A  *  - * Destination: Where to go - * B.txt--Write data--FileOutputStream the  *  - * Java.io.filenotfoundexception:a.txt (the system cannot find the file specified.) ) -  *  - * This time copy Chinese no problem, why? + * The last time we had a problem was because each time we fetched a byte of data, we converted the byte data to character data and then output it to the console.  - * And this time? Really read the data through IO Stream, write to a text file, you read a byte, I write a byte, you do not do any conversion.  + * It will do the conversion on its own.  A  */ at  Public classCopyfiledemo { -      Public Static voidMain (string[] args)throwsIOException { -         //Encapsulating Data Sources -FileInputStream FIS =NewFileInputStream ("A.txt"); -         //Package Destination -FileOutputStream fos =NewFileOutputStream ("B.txt"); in  -         intby = 0; to          while(by = Fis.read ())! =-1) { + Fos.write (by); -         } the  *         //Freeing resources (anyone can do it first) $ fos.close ();Panax Notoginseng fis.close (); -     } the}

Run the effect as follows:

Java Fundamentals Hardening IO Flow Note 22:fileinputstream/fileoutputstream Copy text file case

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.