inheritance diagram for byte stream
The main byte streams are:
Inputstream/outputstream: This is the base class, which is an abstract class.
Fileinputstream/fileoutputstream: The input source and output target are streams of files.
Bytearrayinputstream/bytearrayoutputstream: The input source and output destination are streams of byte arrays.
Datainputstream/dataoutputstream: Adornment class, by base type and string instead of just byte read-write stream.
Bufferedinputstream/bufferedoutputstream: Decorative class that provides buffering capabilities for input and output streams.
First, Inputstream/outputstream
Read reads the next byte from the stream, the return type is int, but the value is between 0 and 255, and when the end of the stream is read, the return value is-1
ii.. fileinputstream/fileoutputstream file Stream
the main construction methods of FileOutputStream are :
Public fileoutputstream (file file) throws FileNotFoundException
Public fileoutputstream (File file, Boolean append) throws FileNotFoundException
Public FileOutputStream (String name) throws FileNotFoundException
Public FileOutputStream (String name, Boolean append) throws FileNotFoundException
From the construction method can be obtained: can be a file object, or the path name.
Note: The Append parameter specifies whether to append or overwrite, true to append, and false by default.
the main construction methods of FileInputStream are :
Public FileInputStream (String name) throws FileNotFoundException
Public fileinputstream (file file) throws FileNotFoundException
Demo Code implementation file copy
1 Public Static voidMain (string[] args) throws IOException {2InputStreaminch=NewFileInputStream ("d:\\mys\\2.jpg");3File copy =NewFile ("d:\\mys\\2copy.jpg");4OutputStream out=Newfileoutputstream (copy);5 byte[] B =New byte[Ten];6 intLen =0;7 while (len = In.read (b))! =-1) {8 Out.write (b, 0, Len); 9}Ten inch. Close (); One out. Close (); A}
Creating a file Stream object actually opens the file, and then the operating system allocates the related resources. If the current user does not have write permissions, an exception SecurityException (parent class RuntimeException) is thrown. If the specified file is an existing directory, or if the file cannot be opened for other reasons, an exception filenotfoundexception is thrown, which is a subclass of IOException.
Three,bytearrayinputstream/bytearrayoutputstream byte array stream
The output target of the bytearrayoutputstream is a byte array whose length is dynamically expanded according to the data content. It has two methods of construction:
Public Bytearrayoutputstream ()
public bytearrayoutputstream (int size)
SIZE specifies the initial array size (default 32)
The data in the Bytearrayoutputstream can also be easily written to another outputstream:
Public synchronized void WriteTo (OutputStream out) throws IOException
Bytearrayinputstream wraps a byte array as an input stream, which is an adapter pattern that is constructed as follows:
Public Bytearrayinputstream (byte buf[])
Public Bytearrayinputstream (byte buf[], int offset, int length)
Length byte data that can be added starting with offset in BUF
iv.
datainputstream/dataoutputstream Data manipulation Flow
Allows applications to read basic Java data types from the underlying input stream in a machine-independent manner.
DataOutputStream Method of Construction:
Public DataOutputStream (OutputStream out)
DataInputStream Method of Construction:
Public DataInputStream (InputStream in)
The order of writing is consistent with the order of reading
Note: DataInputStream is a subclass of the adornment class base class FilterInputStream, FilterInputStream is a subclass of InputStream.
V. Bufferedinputstream/bufferedoutputstream Buffer Stream
buffer flow to "socket" on the corresponding node stream, the data read and write to provide a buffer function, improve the efficiency of reading and writing, while adding some new methods
bufferedinputstream inside a byte array as a buffer, read, first read from this buffer, buffer read and then call the wrapper stream read, it has two construction methods:
Public Bufferedinputstream (InputStream in)
Public Bufferedinputstream (InputStream in, int size)
Size represents the buffer size, and the default value is 8192.
There is a byte array inside the Bufferedoutstream as a buffer, and it is constructed in two ways:
Public Bufferedoutstream (InputStream in)
Public Bufferedoutstream (InputStream in, int size)
Size represents the buffer size, and the default value is 8192.
Using the output stream buffered input stream for file replication
1 Public Static voidMain (string[] args) throws IOException {2InputStreaminch=NewFileInputStream ("d:\\mys\\2.jpg");3File copy =NewFile ("d:\\mys\\2copy.jpg");4OutputStream out=Newfileoutputstream (copy);5Bufferedinputstream BFI =NewBufferedinputstream (inch);6 byte[] B =New byte[Ten];7 intLen =0;8 while(len = Bfi.read (b))! =-1) {9 out. Write (b,0, Len);Ten } One out. Close (); A //when the buffered stream is closed, the underlying node stream that it wraps is automatically closed - bfi.close (); -}
Write data to a file
1 Public Static voidMain (string[] args) throws IOException {2FileOutputStream fos=NewFileOutputStream ("BOSDemo.txt");3Bufferedoutputstream bos=NewBufferedoutputstream (FOS);4String content="I am the buffered output stream test data! ";5Bos.write (Content.getbytes (),0, Content.getbytes (). length);6 //The buffered stream of output, the data written out is cached in memory first, using flush () will cause the in-memory data to be written out immediately7 Bos.flush ();8 bos.close ();9}
Specialized class libraries such as Apache Commons IO
java--Byte stream