Basic knowledge of Java I/O Systems

Source: Internet
Author: User

The abstract concept of stream is often used in I/O class libraries of programming languages. It represents any data source object that has the ability to generate data or the receiving end object that has the ability to receive data. The concept of "stream" is derived from the pipeline pipe in UNIX. In UNIX, a pipe is an uninterrupted byte stream used for communication between programs or processes, or reading and writing peripheral devices and external files, it shields data processing details from actual I/O devices. A stream must be a source end and a destination end. It can be a certain area of computer memory, a disk file, or even a URL on the Internet. The direction of the stream is important. Based on the direction of the stream, the stream can be divided into two types: input stream and output stream. In fact, the input/output is intended for memory. In fact, the source and destination of a stream can be viewed as producers and consumers of bytes. For an input stream, you do not have to worry about its source, as long as data is read from the stream, but the output stream does not know its target end, it simply writes data to the stream.

I. classes in the java. io package correspond to two types of streams

A type of stream reads or writes directly from a specified location, such as a disk file or memory area. This type of stream is called a node stream, and other streams are called a filtered stream packaging stream)

Filter stream: Some streams can receive bytes from files and other places, while others can combine bytes into more useful data types. A constructor that passes an existing stream to another stream. The two streams are combined and the combined stream is called a filter stream. The input stream of the filter is often used as the input source of another input stream. After filtering or processing, it is provided to the user in the form of a new input stream. The output stream of the filter is also similar. We seldom use a single class to create stream objects. Instead, we use multiple objects to provide the desired function, that is, the decoration design mode ).

Java's common input and output streams are inherited from four abstract classes:

  • Based on a single-byte InputStream, The OutputStream class is oriented to byte I/O)
  • Reader based on double-byte Unicode code units, Writer class character-oriented I/O)

Once the input stream is opened, the program can read data from the input stream in sequence. The process of reading/writing data from an input stream is generally as follows: Open a channel --> read/write information --> close the channel.

On the java platform, you can obtain the character encoding type of the local platform in the following two ways:

A) System. getProperty ("file. encoding ");

B) Charset cs = Charset. defaultCharset ();

All input streams and output streams can be divided into byte input and output streams, character input and output streams, processing byte mainly OutputStream/InputStream) series, processing character, mainly Reader/Write) Series

2. Byte-oriented input stream InputStream series). These classes can be connected to FileInputStream objects to provide useful interfaces:

Output stream OutputStream series oriented to Byte). It can be connected to a FilterOutputStream object to provide useful interfaces:

3..Corresponding Reader/Writer series:

Iv. Conversion Between Two unlimited stream types (using the adapter class)

InputStreamReader and OutputStreamReader: convert a byte-oriented stream into a character-oriented stream.

InputStreamReader is a bridge between byte stream and byte stream: It reads bytes using the specified charset and decodes them into characters. Its character set can be specified by name or explicitly specified, or it can accept the default Character Set of the platform.

OutputStreamWriter is a bridge between the bytes stream and the byte stream. You can use the specified charset to encode the characters in the stream into bytes. The character set used can be specified by the name or explicitly specified. Otherwise, the default Character Set of the platform will be accepted.

5. read data from InputStream through FilterInputStream:

Write Data to OutputStream through FilterOutputStream:

6. Change the stream Behavior

Although BufferedOutputStream is a subclass of FilterOutputStream, BufferedWriter is not a subclass of FilterWriter, and FilterWriter is an abstract class without any subclass)

7. Self-independent class: RandomAccessFile

This class applies to files composed of records of known sizes. RandomAccessFile not only implements the DataInput and DataOutput interfaces DataInputStream and DataOutputStream, but also implements these two interfaces, this class is a completely independent class, which has essentially different behaviors from other I/O types. It can be moved before and after a file and derived directly from the Object.

Most functions of RandomAccessFile are not all) are replaced by nio storage ing files.

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.