Java I/O summary

Source: Internet
Author: User

 

Io is a very important piece, but it is often overlooked, and hopefully there is a very clear introduction to it. You learn at the same time want to be able to bring some help to others, the article used to other authors (has given the link) pictures here to express thanks!

Classification of IO

Java IO can be divided into two categories:
(1) Byte operation-based IO interface: InputStream and OutputStream
(2) IO interface based on character manipulation: Writer and Reader
Both disk and network transmissions, their smallest units are bytes, but most of the data in the program is presented as characters, so Java also provides support for character streams. Another thing to note is that the way data is transmitted is also important, that is, the problem of where the data is written, usually in one of two ways:
1. Disk operation-based IO interface: File
2. Network operation-based IO interface: Socket

The overall structure of the IO stream


Image reference from (Reference block content)

Class hierarchies for InputStream

Class hierarchies for OutputStream

Quote from (here to write the link content)

The hierarchical relationship between reader and writer class

Reader is the base class for all reader in Java IO. Similar to Inpustream, the difference is that reader is character-based, that is, reader is used to read text, and Inpustream is used to read the original bytes.

Java uses UTF8 encoding to represent strings, which you need to be aware of when converting between a byte stream and a character stream. If you read UTF8 encoded text from one byte in the input stream and try to convert the bytes read to a character, this can be an error!

Writer is the base class for all writers in Java IO, and the same writer is based on characters rather than bytes for writing text.


Image reference from (here to write link content)

Conversion of Byte stream and character stream

Data persistence and network transmission are all in the form of bytes, so there is a problem of conversion between the byte stream and the character stream. Conversions are divided into two directions: byte-to-character conversions and character-to-byte conversions. The main concern here is coding and decoding, for example, the Inputstream.read () method reads bytes in the data stream (read () reads only one byte at a time, is very slow, we generally use Read (byte[])), and then saves in a byte[] Array, and the last converted to string. When we read a file, the encoding of the read byte depends on the encoding format used by the file, and the problem of encoding is also involved in converting to string, which can occur if the encoding format is different between the two. For example, if there is a *. TXT encoding format of UTF-8, then the data stream encoding format obtained by reading the file through the byte stream is UTF-8, and if we do not specify an encoding format when we convert to string, the system encoding format (GBK) is used by default to decode the operation. Because the encoding format is inconsistent, the construction of the string process will certainly produce garbled characters.

The bridge that converts bytes into characters is inputstreamreader, and he reads the bytes with the specified charset and decodes them into characters. We can either specify the encoding or accept the platform's default character set.

Base class InputStream and its implementation class

Image reference from (here to write link content)

Construction Method:
InputStream () create an input stream stream
Other methods
1. Close (): Closes the stream method, which is the method that must be called each time the stream is exhausted.
2. Read (): The method reads a byte byte, but returns an int.
3. Read (byte[]): Read content once to buffer byte array
4. Read (Byte[],int,int): from where in the data stream offset begins reading Len-length content to buffer byte array
5. Skip (Long): Skips a long type parameter from the stream

Implementation class of InputStream

@ByteArrayInputStream – Use one of the buffers in memory as InputStream.

Construction method
A Bytearrayinputstream (byte[]) creates a new byte array input stream (Bytearrayinputstream) that reads data from the specified byte array (using byte as an array of buffers)
B Bytearrayinputstream (byte[], int, int) creates a new byte array input stream that reads data from the specified byte array.
Example:
byte[] buff = new byte[] {2, 15, 67,-1,-9, 9};
Bytearrayinputstream in = new Bytearrayinputstream (buff, 1, 4);

@StringBufferInputStream – Put a String object as InputStream.

Construction method
StringBufferInputStream (string) creates an input stream string that reads data from a specified string.

@ fileinputstream– a file as a inputstream for read operations to the file

Construction method
A FileInputStream (file name) creates an input file stream that reads data from the specified file object.
B FileInputStream (filedescriptor) creates an input file stream that reads data from the specified file descriptor.
(C)-fileinputstream (String name) creates an input file stream that reads data from a file of the specified name.

@PipedInputStream: The concept of pipe is realized, which is used in the main thread. Pipeline inflow refers to the receiving end of a communication pipeline.

Construction method
A PipedInputStream () creates a pipeline input stream that is not yet connected to a pipeline output stream.
B PipedInputStream (PipedOutputStream) creates a pipeline input stream that is connected to a pipeline output stream.

@ Sequenceinputstream: Merge multiple inputstream into one inputstream. The sequence input stream class allows applications to merge several input streams continuously and make them appear as a single input stream. Each input stream is read sequentially until the end of the stream is reached. The sequence input stream class then closes the stream and automatically switches to the next input stream.

Construction method
Sequenceinputstream (enumeration) creates a new sequence input stream and initializes it with the enumeration value of the specified input stream.
Sequenceinputstream (InputStream, InputStream) creates a new sequence input stream, initializes the read input stream s1 first, and then reads the input stream s2.

@ObjectInputStream: The stream used to manipulate object, which is used primarily in the process of transferring objects, which involves the knowledge of serialization

Construction method
ObjectInputStream (): Instantiate a ObjectInputStream object
ObjectInputStream (InputStream): Uses a InputStream object to instantiate a ObjectInputStream object, where InputStream is the input stream of the object

@ filterinputstream: is a filtered InputStream

Construction method
FilterInputStream (InputStream): Use a inputstream to instantiate a filterinputstream for a parameter, which is an adorner pattern that can be discerned from the way in which his subclasses are constructed. He has the following sub-categories:

1) Bufferedinputstream: Stream using buffer
Construction method
Bufferedinputstream (InputStream): Initializes an instance of a parameter with InputStream
Bufferedinputstream (inputstream,int): Ability to set buffer size Bufferedinputstream

2) DataInputStream: Digitally formatted stream
Construction method
DataInputStream (InputStream): Initializes an instance with the InputStream parameter

Base class OutputStream

OutputStream
Construction method
OutputStream ()
Other methods
1. Write (int): Writes a byte into the stream
2. Write (byte[]) writes a byte array into the stream
3. Write (Byte[],int,int): Writes a byte array from the beginning of offset to a len-length data
4. Close (): Close the stream, this is the method that must be called after the stream is finished
5. Flush (): This method is used to flush the data in the stream so that the data in the buffer forces the output
The inherited subclasses are as follows:

@ByteArrayOutputStream: The information is stored in a buffer in memory. This class implements an output stream that writes data as a byte array.

Construction method
(a)-bytearrayoutputstream () Creates a new byte array output stream.
(B)-bytearrayoutputstream () Creates a new byte array output stream.
(C)-bytearrayoutputstream (int) Creates a new byte array output stream with a buffer capacity of the specified size byte.

@ fileoutputstream: A file output stream is an output stream that outputs data to file or FileDescriptor

Construction method
A FileOutputStream (file name) creates a file output stream that outputs data to the specified file object.
B FileOutputStream (filedescriptor) creates a file output stream that outputs data to the specified file descriptor.
C FileOutputStream (String name) creates a file output stream that outputs data to a file of the specified name.
D FileOutputStream (String, Boolean) creates an output file with the file name of the specified system.

@ PipedOutputStream: The pipeline output stream refers to the sending side of a communication pipeline. A thread sends data through a pipeline output stream,
The other thread reads the data through a piped stream, which enables communication between two threads.

Construction method
A PipedOutputStream () creates a pipeline output stream that has not yet been connected to a pipeline's inflow.
B PipedOutputStream (PipedInputStream) creates a pipeline output stream that is connected to a pipeline input stream.

@ObjectOutputStream: Output The stream of object objects

Construction method
ObjectOutputStream: Instantiating a ObjectOutputStream object
ObjectOutputStream (OutputStream): Use OutputStream object to instantiate a ObjectOutputStream object and write the object to OutputStream

@FilterOutputStream: Filter The OutputStream, and the next few are his sub-categories

Construction method
Filteroutputstream (OutputStream): Instantiate with OutputStream parameter Filteroutputstream

@BufferedOutputStream: Stream with buffer

Construction method
Bufferedoutputstream (outputstream): Initialize Bufferedoutputstream class with OutputStream parameter
Bufferedoutputstream (outputstream,int): Specifies the size of the buffer when initializing the instance

@ DataOutputStream: Formatted OutputStream

Construction method
DataOutputStream (OutputStream): Initializes the DataOutputStream instance with the OutputStream parameter

@PrintStream: Direct output to the console: what we're most familiar with is System.out, he's a printstream.

Construction method
PrintStream (OutputStream): use outputstream parameter to instantiate PrintStream

Character Stream Stream--reader and writer

Reader base class: Stream based on character stream
constructor function
Reader (): Method of constructing without parameters

@CharArrayReader: Corresponds to this class to implement a character buffer that can be used as a character input stream for Bytearrayinputstream
constructor function
CharArrayReader (char[]) creates a chararrayreader with the specified character array.
CharArrayReader (char[], int, int) creates a chararrayreader with the specified character array

@ StringReader: A stream of characters corresponding to StringBufferInputStream whose source is a string.

@ FileReader: corresponds to FileInputStream

@ pipedreader: corresponds to PipedInputStream

@ inputstreamreader: convert InputStream into reader

Base class writer

@CharArrayWriter: Corresponds to Bytearrayoutputstream

@ stringwriter: No byte-oriented stream corresponding to it

@ FileWriter: corresponds to FileOutputStream

@ pipedwriter: corresponds to PipedOutputStream

@ outputstreamwriter: Convert OutputStream to writer

@ printreader: Corresponds with PrintStream

Conversion between a byte stream and a character stream stream
@ InputStreamReader and Outputstreamreader:
Converts a byte-oriented stream into a character-oriented stream.
The InputStreamReader class is a bridge that flows from a byte to a character stream: it reads in bytes and converts it to a character stream based on the specified encoding.
The encoding used may be specified by name, or the platform can accept the default encoding method.
Each invocation of one of the InputStreamReader's read () methods may cause one or more bytes to be read from the base byte input stream.
In order to achieve greater efficiency, consider packaging inputstreamreader with BufferedReader,
BufferedReader in = new BufferedReader (new InputStreamReader (system.in));

Java I/O summary

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.