Example 1 is an example of using fileinputstream and fileoutputstream. the program can copy the file. It will first read data from the source file to a byte array, and then write data from the byte array to the target file. example 1: filestreamdemo. javaimport Java. io. *; public class filestreamdemo {public static void main (string [] ARGs) {try {// to source file fileinputstream in = new fileinputstream ("D:/B .txt "); // target file fileoutputstream out = new fileoutputstream ("C:/a.txt"); byte [] bytearray = new byte [1024]; do {In. read (bytearray, 0, 1 024); out. write (bytearray);} while (in. available ()> 0); In. close (); out. close ();} catch (arrayindexoutofboundsexception e) {e. printstacktrace ();} catch (ioexception e) {e. two read () methods are demonstrated in the printstacktrace () ;}} program. One method can read data of the specified length to an array, and the other method can read one byte at a time. after each read operation, the cursor is directed forward. If no data is read, the system returns-1. the available () method can be used to obtain the number of bytes that can be read. when you do not use a file stream, remember to close the stream by using the close () method to release system resources that are dependent on the stream. by default, fileoutputstream starts the stream by creating a new file. if the specified file name already exists Yes, the original file will be overwritten. If you want to write files in the additional mode, you can specify the additional mode when creating the fileoutputstream instance. for example, fileoutputstream = new fileoutputstream (ARGs [1], true); if the second append parameter of the build method is set to true, when the stream is enabled, if the file does not exist, a new file is created. If the file exists, the stream is directly enabled and the written data is appended to the end of the file. ---------------------------------------------------------------------------------------------------------------------- bufferedinputstream and bufferedoutputstream Java. io. bufferedinputstream and Java. io. bufferedoutputstream can add the buffer function for inputstream and outputstream objects. When constructing a bufferedinputstream instance, you must specify an inputstream instance. When bufferedinputstream is implemented, the inputstream instance is actually implemented. similarly, when constructing a bufferedoutputstream, you also need to specify an outputstream instance to implement an outputstream instance. the data member Buf of bufferedinputstream is a single-digit group. The default value is 2048 bytes. The data member Buf of bufferedoutputstream is also a single-digit group. The default value is 512 bytes. example 2 is a rewrite of Example 1. You do not need to set the buffer, which is simple and efficient. example 2 bufferedstreamdemo. Javaimport Java. io. *; public class filestreamdemo {public static void main (string [] ARGs) {try {// to source file fileinputstream in = new fileinputstream ("D:/B .txt "); // target file fileoutputstream out = new fileoutputstream ("C:/a.txt"); bufferedinputstream bufferedin = new bufferedinputstream (in); writable bufferedout = new bufferedoutputstream (out ); byte [] DATA = new byte [1]; while (bufferedin. re AD (data )! =-1) {bufferedout. write (data);} // write all the data in the buffer to bufferedout. flush (); // close the stream bufferedin. close (); bufferedout. close ();} catch (arrayindexoutofboundsexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} to ensure that the data in the buffer zone must be written to the destination, we recommend that you run flush () to write all the data in the buffer zone to the destination stream, the execution result of this example is the same as that of Example 1.