Summary: fileinputstream and fileoutputstream. bufferedinputstream and bufferedoutputstream

Source: Internet
Author: User

Fileinputstream is a subclass of inputstream. It can be known from the name that fileinputstream mainly reads data from the specified file to the destination.

Fileoutputstream is a subclass of outputstream. As its name suggests, fileinputstream writes data from the source to the specified file.

The standard input/output streaming object is enabled at the beginning of the program, but the actual streaming is enabled only when you create a fileinputstream or fileoutputstream instance, you must also disable streaming to release system resources that are dependent on streaming.

The following program can copy files. The program first reads data from the source file to a one-bit meta buffer, and then writes the data of the bit meta array to the target file:

  • Filestreamdemo. Java
Package onlyfun. Caterpillar;

Import java. Io .*;

Public class filestreamdemo {
Public static void main (string [] ARGs ){
Try {
Byte [] buffer = new byte [1024];

Fileinputstream =
New fileinputstream (new file (ARGs [0]);
Fileoutputstream =
New fileoutputstream (new file (ARGs [1]);

System. Out. println ("copy file:" +
Fileinputstream. Available () + "bitkey ");
While (true) {// read data from the source file to the buffer zone
If (fileinputstream. Available () <1024 ){
Int remain;
While (remain = fileinputstream. Read ())
! =-1 ){
Fileoutputstream. Write (remain );
}
Break;
}
Else {
Fileinputstream. Read (buffer );
// Write the array data to the target file
Fileoutputstream. Write (buffer );
}
}

// Disable streaming
Fileinputstream. Close ();
Fileoutputstream. Close ();

System. Out. println ("copied ");
}
Catch (arrayindexoutofboundsexception e ){
System. Out. println (
"Using: Java filestreamdemo SRC des ");
E. printstacktrace ();
}
Catch (ioexception e ){
E. printstacktrace ();
}
}
}

This program demonstrates two read () methods. One can read data of the specified length to the array, and the other can read a single bitkey at a time. After each read, The read indicators will go forward, you can use the available () method to obtain the number of tuples that can be read. In addition to using file to create fileinputstream and fileoutputstream instances, you can also directly use the specified path of the string to create one.

When streaming is not used, remember to close the stream by using the close () method to release system resources that are dependent on the stream.
 

 

Bufferedinputstream and bufferedoutputstream

 

In the case of Introduction to fileinputstream and fileoutputstream, you use an array as the buffer for Data Reading. Taking file access as an example, you know that the disk access speed is much lower than the data access speed in the memory. To reduce disk storage, you read data of a certain length at a time. The 1024-bit tuples in the above topic example, the write operation also writes data of a certain length at a time, which increases the efficiency of data access.

Bufferedinputstream and bufferedoutputstream can be used to add the buffer function for inputstream objects. You do not need to design the buffer.

Buf, a data member of bufferedinputstream, is a single-digit array with a default size of 2048-bit tuples. When reading data sources, such as files, bufferedinputstream will try to fill up the Buf. When read () the method is to read the data in the Buf first, instead of directly reading the data source. When the data in the Buf is insufficient, bufferedinputstream will then extract the data from the data source.

Buf, a data member of bufferedoutputstream, is a single-digit array with a default value of 512. When writing data, it first saves the data to the Buf, only when the Buf is full will the data be written to the destination at a time, instead of writing data to the destination at a time.

Rewrite the example of the previous topic. This time, you don't have to set the buffer and judge it. Using bufferedinputstream and bufferedoutputstream makes the program simpler and more efficient:

  • Bufferedstreamdemo. Java
Package onlyfun. Caterpillar;

Import java. Io .*;

Public class bufferedstreamdemo {
Public static void main (string [] ARGs ){
Try {
Byte [] DATA = new byte [1];
File srcfile = new file (ARGs [0]);
File desfile = new file (ARGs [1]);

Bufferedinputstream =
New bufferedinputstream (
New fileinputstream (srcfile ));
Bufferedoutputstream =
New bufferedoutputstream (
New fileoutputstream (desfile ));

System. Out. println ("copy file:" +
Srcfile. Length () + "bitkey ");
While (bufferedinputstream. Read (data )! =-1 ){
Bufferedoutputstream. Write (data );
}

// Write all data in the buffer zone
Bufferedoutputstream. Flush ();

// Disable streaming
Bufferedinputstream. Close ();
Bufferedoutputstream. Close ();
}
Catch (arrayindexoutofboundsexception e ){
System. Out. println (
"Using: Java usefilestream SRC des ");
E. printstacktrace ();
}
Catch (ioexception e ){
E. printstacktrace ();
}
}
}

To ensure that the data in the buffer zone is written, we recommend that you finally execute flush () to write all the data in the buffer zone into the destination stream.

Bufferedinputstream and bufferedoutputstream do not change the behaviors of the source inputstream or destination outputstream. When reading or writing data, they are still the actions of inputstream and outputstream, bufferedinputstream and bufferedoutputstream dynamically add some functions (such as the buffer function) to them before that. Here, we use the File Access stream as an example, in fact, you can add bufferedinputstream and bufferedoutputstream to other streaming objects.

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.