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