Java Fundamentals of I/O systems

Source: Internet
Author: User

The abstract concept of flow is often used in the I/O class Library of programming languages, which represents any data source object that has the ability to produce data or a receive-side object that has the ability to receive data. The concept of "flow" stems from the concept of piping (pipe) in Unix.   In Unix, a pipeline is an uninterrupted stream of bytes that is used to communicate between programs or processes, or to read and write peripherals, external files, and so on, which masks the details of processing data in an actual I/O device. A stream, which must have both the source and destination, can be some area of computer memory, a disk file, or even a URL on the Internet. The direction of the flow is important, depending on the direction of the flow, the flow can be divided into two categories: the input stream and the output stream. The input/output is actually about memory. In fact, the source and destination of a stream can simply be seen as a producer and consumer of bytes, and for an input stream, you don't have to care what its source is, simply read the data from the stream, and on the output stream, you don't know its destination, but simply write the data to the stream.

A. Flow: An ordered set of data sequences.

B-byte throttling: the smallest data unit in the data stream is bytes.

C. Character stream: The smallest data cell in the data stream is a character.

I. Classes in the Java.io package correspond to two types of flow

A class of streams is read or written directly from a specified location (such as a disk file or memory region), which is called a node stream, and other streams are called filter streams (wrapper streams)

Filter streams: Some streams can receive bytes from files and elsewhere, and others can combine bytes into more useful data types. The construction method of passing an already existing stream to another stream, combining the two streams, is called the filtered stream. The filter input stream is often used as its input source for other input streams, filtered or processed and then provided to the user in the form of a new input stream, which is similar to the filter output stream. We seldom use a single class to create a flow object, but rather by overlapping multiple objects to provide the desired functionality (that is, the adorner design pattern).

Java's common input and output streams are actually inherited from 4 abstract classes, namely:

Inputstream,outputstream class based on a single byte (byte-oriented I/O)

Reader based on double-byte Unicode code unit, writer class (I/O for character type)

Once the input stream is opened, the program can read the data sequentially from the input stream. The process of reading/writing data from the input stream is generally as follows: Open a flow Channel--read/write information--close the circulation channel.

In the Java platform, there are two ways to get the character encoding type of the local platform:

(a) System.getproperty ("file.encoding");

(b) Charset cs=charset.defaultcharset ();

All input streams, output streams can be divided into bytes (input, output) stream, character (input, output) stream, the main processing byte is (Outputstream/inputstream) series, processing characters, mainly (Reader/write) series

Two. Byte-oriented input stream (InputStream series), these classes can be connected to the FileInputStream object to provide a useful interface:

Bytearrayinputstream: Use a buffer in memory as a InputStream

StringBufferInputStream (deprecated in java1.1): Takes a String object as a InputStream, and the underlying implementation uses StringBuffer

FileInputStream: To use a file as a inputstream, to implement the read operation (file name, file, FileDescriptor object)

PipedInputStream: Implements the concept of pipe, which is used in the main thread (as a data source in multi-process)

Sequenceinputstream: Merging multiple inputstream into one InputStream

A byte-oriented output stream (OutputStream series) that can be connected to a Filteroutputstream object to provide a useful interface:

Bytearrayoutputstream: Creating buffers in memory, storing information in a buffer in memory, buffer initialization size (optional)

FileOutputStream: Depositing information into a file (filename, file, filedescriptor)

PipedOutputStream: Implements the concept of pipe, which is used in the main thread (specifies the destination of data for multithreading)

Three. The corresponding (Reader/writer) series:

Reader: Corresponds to InputStream, adapter InputStreamReader

Writer: Corresponds to OutputStream, adapter is OutputStreamWriter

FileReader: Corresponds to FileOutputStream

FileWriter: Corresponds to Fileourputstream

StringReader: Non-corresponding classes

StringWriter: Corresponds to Bytearrayinputstream

CharArrayReader: Corresponds to Bytearrayoutputstream

Chararraywriter: Corresponds to Bytearrayoutputstream

Pipedreader: Corresponds to PipedInputStream

PipedWriter: Corresponds to PipedOutputStream

Four. Conversion between two unrestricted-directed streams (using the adapter class)

InputStreamReader and Outputstreamreader: Converts a byte-oriented stream into a character-oriented stream.

InputStreamReader is a bridge of byte flow to a character stream: it reads the bytes with the specified charset and decodes them to characters. The character set it uses can be specified by name or explicitly given, or it can accept the default character set of the platform

OutputStreamWriter is a bridge of character flow to a byte stream: You can encode the characters in the stream to be written into bytes using the specified charset. The character set it uses can be specified by name or explicitly given, otherwise the platform default character set will be accepted

Five. Read data from InputStream via FilterInputStream:

DataInputStream: Reads the base type (int, char,long, etc.) data from the stream.

Bufferedinputstream: Use a buffer to prevent the actual read of each read

Linenumberinputstream: The number of rows in the input stream is recorded and can then be called Getlinenumber () and setlinenumber (int)

Pushbackinputstream: Rarely used, generally for compiler development

Write to OutputStream via Filteroutputstream:

Dataioutputstream: You can export the base type (int, char,long, etc.) data to stream as you migrate.

Bufferedoutputstream: Use a buffer to avoid actual writes every time you send data

PrintStream: produces formatted output, where DataOutputStream handles the storage of data, PrintStream processing displays

Six. Changing the behavior of a stream

Although Bufferedoutputstream is a subclass of Filteroutputstream, BufferedWriter is not a subclass of Filterwriter (Filterwriter is an abstract class, without any subclasses)

There are no classes corresponding to DataInputStream. Use DataInputStream unless you want to use ReadLine () instead of BufferedReader

BufferedReader: Corresponds to Bufferedinputstream

LineNumberReader: Corresponds to Linenumberinputstream

Pushbackreader: Corresponds to Pushbackinputstream

Bufferedwrite: Corresponds to Bufferedoutstream

Printwrite: Corresponds to PrintStream

Seven. Self-contained classes: Randomaccessfile

This class is suitable for files that have known size records, Randomaccessfile in addition to implementing the Datainput and DataOutput interfaces (both DataInputStream and DataOutputStream implement both interfaces). This class is a completely separate class that has behavior that differs fundamentally from other I/O types and can be moved forward and backward within a file, deriving directly from object.

Read and write to a file by Randomaccessfile object when an object is generated, you can indicate the nature of the file to be opened: R, read only, W, write only RW read-write can jump directly to the location specified in the file Randomaccessfile most of the functionality (not all) is superseded by the NIO storage mapping file.

Java Fundamentals of I/O systems

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.