Concept byte stream
In the computer, whether text, images, audio or video, all files are present in binary (byte) form. IO stream provides a series of bytes for the input and output streams, referred to as a byte stream. Byte stream is the most commonly used program stream, which can be entered into byte stream and output stream of bytes of data according to transmission direction. In the JDK, two InputStream and the OutputStream abstract classes, which are the top-level parent of the byte stream, the input stream of bytes are all inherited from InputStream, all bytes output streams are inherited from OutputStream.
InputStream is input as a pipe, the OutputStream is seen as an output pipe, through the data input from the source device to InputStream program output from the program to the target device via the OutputStream, enabling data transmission. IO stream input and output are relative to the program
InputStream common method
public static Read () read from a byte stream data byte public static Read ( byte [] B) from the input data stream will be read up b.length bytes into a byte array public static Read ( byte [] B, int oFF, int len) specified byte array starting at offset off len bytes read stream file os.close () Close the file output stream and releases any system resources associated with this stream relevant
OutputStream common method
public void Write ( int B) The specified byte is written to the file output stream public void Write ( byte [] B) The b.length bytes written to the file from a specified byte array to the output stream public void Write ( byte [] B, int OFF, int len) specified byte array starting at offset off len bytes written to the file output stream flush () refresh buffer and forces any buffered output bytes to write os.close () Closes this file input stream and releases any system resources associated with the stream
InputStream and OutputStream classes while providing these two methods related to a series of read and write data, but these two class is abstract and can not be instantiated, therefore, for different functions, InputStream and OutputStream provide two different subclass, which form a subclass architecture
Write byte stream file
Because computer data base are stored in the hard disk file, so the operation when the data file is a very common operation. In operation file, the most common is to read data from the data file written to the file, i.e. to read and write files. For reading and writing files, JDK provides two special classes are FileInputStream and FileOutputStream.
FileInputStream is a subclass of InputStream, which is byte operation file input stream, specifically for reading the data file. Due to the repetitive operation of data read from the file, it is necessary to achieve continuous data read by the loop.
Next, to achieve the reading of the file data byte streams through a case:
public class example01 { public static void main (String [] args) throws Exception { // create a file input stream of bytes engrave an object the FileInputStream in = new new the FileInputStream ( "test.txt" ); int B = 0; // definition of a variable of type int b, a byte is read each time remember the while ( to true ) { b = in.read (); // variable b remember a byte read IF (b == -. 1) { // byte read is -1 if, out of the while loop BREAK ; } System.out.println (B); //Otherwise, the output b } } }
It corresponds with FileInputStream FileOutputStream. FileOutputStream is a subclass of OutputStream, he byte output file stream operation, specially written to the file input.
Followed by a case study to demonstrate how the data is written to the file:
public class Example02 { public static void main(String[] args) throws IOException { FileOutputStream out = new FileOutputStream("example.txt"); String str="波音飞机"; byte[] b = str.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } out.close(); } }
Note that, if it is to write data to a file that already exists by FileOutputStream, then the data in the file first will be the case, and then write data. If you want additional content after the new content file already exists, you can use the constructor FileOutputStream FileOutputStrem (String file, boolean append) to create a file output stream object, and set the value append the parameter to true.
Followed by a case study to demonstrate how to append data to the end of the file
public class example01 { public static void main (String [] args) throws Exception { // create a file input stream of bytes engrave an object the FileInputStream in = new new the FileInputStream ( "test.txt" ); int B = 0; // definition of a variable of type int b, a byte is read each time remember the while ( to true ) { b = in.read (); // variable b remember a byte read IF (b == -. 1) { // byte read is -1 if, out of the while loop BREAK ; } System.out.println (B); //Otherwise, the output b } } }
Since during data stream IO write operation will be abnormal, for brevity code used keywords throws the above program will be thrown exception. However, when confronted with IO exception, close IO stream () method will not be executed, the stream object occupied system resources will not be released, therefore, in order to ensure close IO stream () method must be executed, usually close the stream write operation in a finally block
finally{ try{ if(in!=null){ in.close; }catch(Exception e){ e.printStackTrace(); } } }
Copies of documents
In application, the IO stream usually occur in pairs, i.e. with the use of the input and output streams. For example, copies of documents need to read the data file through the input stream, written to the file by the file output stream.
Next to demonstrate how to copy a file's contents by case:
public class example01 { public static void main (String [] args) throws Exception { // Create an input stream of bytes, for reading a file in the current directory map3 sourse folder the FileInputStream in = new new the FileInputStream ( "\\ sourse Week deep - fish .mp3 " ); // Create a output stream of bytes, the byte is used to write the read file in the directory target a FileOutputStream OUT = new new a FileOutputStream (" target deep circumferential \\ - large fish .mp3 " ); int len; // define a variable of type integer len, remember that every time a byte is read Long startTime = System.currentTimeMillis (); // Get copied file system time before the while ( (len = in.read ()) = -! 1) { Out.write (len); // The read byte written to the file } Long the endtime = System.currentTimeMillis (); System.out.println ( "copy time:" + (endtime-starttime) + " ms " ); in.close (); the out.close (); } }
Buffer byte stream
When copying a file, you can read more bytes of data one time, and stored in a byte array, and the array of data bytes written to the file once.
Next, learn how to use a buffer by modifying the file to copy files
public class Example02 { public static void main (String [] args) throws Exception { // create a file input stream for reading mp3 files in the current directory in the directory sourse the FileInputStream in = new new the FileInputStream ( "\\ circumferential deep sourse - fish .mp3 " ); // creates a file output stream for writing data to the read target file in the current directory, a FileOutputStream OUT = new new a FileOutputStream (" target deep circumferential \\ - fish .mp3 " ); // the following files are used to read and write buffers byte [] = BUFF new new byte [1024]; // define an array of bytes as a buffer int len; Long the startTime =System.currentTimeMillis (); the while (! (Len = in.read (BUFF)) = -. 1) { // determines whether the end of file read out.write (BUFF, 0, len); // from the first word section start, to write the file len characters } Long endTime = System.currentTimeMillis (); System.out.println ( "copies of documents time used:" + (endTime- the startTime)); in.close (); OUT. Close (); } }
In the copy process, gradually implemented while loop copy bytes of the file, once per cycle, a number of bytes to read from the file byte array is filled, and the number of bytes read by the array variable len remember, and then from the array the first byte, the first len bytes written to the file. Cycle, when len -1 if the end of the file has been read, the cycle will end, the whole copy process is over.
The program is a buffer memory, the memory is mainly used to temporarily store data input and output, the use of buffers reduces the number of file operations, can improve the efficiency of all read and write data.
Provided with two packets buffered in the IO byte stream, and BufferedOutputStream The BufferedInputStream respectively, their construction process receive InputStream and OutputStream type parameter as an object to provide a buffering function when reading and writing data.
Next to learn and use BufferedInputStream BufferedOutputStream the two streams by a case:
public class Example03 { public static void main (String [] args) throws Exception { // create a buffer with an input stream BufferedInputStream BIS = new new BufferedInputStream ( new new the FileInputStream ( "src.txt" )); // a buffer zone created output stream region BufferedOutputStream the BOS = new new BufferedOutputStream the ( new new a FileOutputStream ( "des.txt" )); int len; the while ((len = bis.read ()) = -. 1! ) { bos.write (len); } BIS .close (); BOS .close (); } }
When the call read () or write () method for reading and writing data, the write data is first stored in a well defined byte array, and then writes the byte array into a one-time file. Thereby improving the efficiency of data reading and writing