JAVA input/output stream instance (fileinputstream and fileoutputstream)

Source: Internet
Author: User
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.

 

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.