Java basic knowledge I/O flow usage detailed _java

Source: Internet
Author: User

The concept of "flow" originates from the concept of pipelines (pipe) in Unix.   In Unix, a pipeline is an uninterrupted stream of bytes 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 the actual I/O device. A stream, which must have a source and destination, can be some area of the computer's memory, or it can be a disk file, or even a URL on the Internet. The direction of the flow is important, according to the direction of the flow, the flow can be divided into two categories: input stream and output stream. In fact, the input/output is to the memory. In fact, the source and destination of the stream can be simply viewed as a byte of producer and consumer, for an input stream, you do not have to care about what its source is, as long as you simply read the data from the stream, and you do not know the destination of the output stream, but simply write the data to the stream.

A. Flow: A set of ordered data sequences.
B-byte throttling: the smallest data cell in the data stream is bytes.
C. Character stream: The smallest data cell in a data flow is a character.

the class in the Java.io package corresponds to two types of flows

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

Filter Flow: Some streams can receive bytes from files and elsewhere, and others can combine bytes into more useful data types. By passing an already existing stream to the construction method of another stream, the two streams are combined, and the combined stream is called a filtered stream. The filter input stream is often the other input stream as its input source, filtered or processed and then provided to the user in the form of a new input stream, and the filter output stream is similar. Rather than using a single class to create a stream object, we provide the desired functionality (that is, the adorner design pattern) by stacking multiple objects.

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

Inputstream,outputstream Class (Byte-oriented I/O) based on Single-byte
Reader of Unicode code unit based on Double-byte, writer class (character-oriented I/O)
Once the input stream is opened, the program can sequentially read the data from the input stream. The process of reading/writing data from an 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 stream, output stream can be divided into bytes (input, output) stream, character (input, output) stream, processing byte is mainly (Outputstream/inputstream) series, processing characters, mainly (Reader/write) series

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

Bytearrayinputstream: Use a buffer in memory as a InputStream
StringBufferInputStream (deprecated in java1.1): A String object is used as a inputstream, and the underlying implementation uses the StringBuffer
FileInputStream: A file as a inputstream, implementation of the file read operations (file name, file, FileDescriptor object)
PipedInputStream: Implements the concept of pipe, used primarily in threads (as a data source in multiple processes)
Sequenceinputstream: Combine multiple inputstream into one InputStream
A byte (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, initializing buffer size (optional)
FileOutputStream: Put the information in the file (file name, file, FileDescriptor)
PipedOutputStream: Implements the concept of pipe, which is used in the main thread (specifying destinations for multi-threaded data)

Three. corresponding to (reader/writer) series :

Reader: Corresponds to InputStream, adapter InputStreamReader
Writer: Corresponds to OutputStream, adapter is OutputStreamWriter
FileReader: corresponding to the FileOutputStream
FileWriter: corresponding to the Fileourputstream
StringReader: None of the corresponding classes
StringWriter: corresponding to the Bytearrayinputstream
CharArrayReader: corresponding to the Bytearrayoutputstream
Chararraywriter: corresponding to the Bytearrayoutputstream
Pipedreader: corresponding to the PipedInputStream
PipedWriter: corresponding to the PipedOutputStream

Four. conversion between two types of open stream ( using adapter classes)

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

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

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

V. reading data from InputStream through FilterInputStream :

DataInputStream: Reads the base type (int, char,long, etc.) data from the stream.
Bufferedinputstream: Use a buffer to prevent the actual read operations from being read every time
Linenumberinputstream: Records the number of rows in the input stream, and then calls Getlinenumber () and setlinenumber (int)
Pushbackinputstream: Rarely used, commonly used in compiler development
Write to OutputStream by Filteroutputstream:

Dataioutputstream: The basic type (int, char,long, etc.) data can be exported to the stream according to the migration method.
Bufferedoutputstream: Use buffers to avoid actual write operations every time you send data
PrintStream: Produces formatted output, in which DataOutputStream processing data storage, PrintStream processing display

Six. change the behavior of the stream

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

There is no class corresponding to the DataInputStream. Use BufferedReader unless you want to use ReadLine () instead of DataInputStream
BufferedReader: Correspondence with Bufferedinputstream
LineNumberReader: Correspondence with Linenumberinputstream
Pushbackreader: Correspondence with Pushbackinputstream
Bufferedwrite: Correspondence with Bufferedoutstream
Printwrite: Correspondence with PrintStream

Seven. Self-independent class: Randomaccessfile

This class applies to files that have a known size record, randomaccessfile in addition to implementing the Datainput and DataOutput interfaces (DataInputStream and DataOutputStream also implement these two interfaces), This class is a completely separate class that has behavior that is different from the nature of other I/O types and can be moved forward and backward within a file, deriving directly from object.

Can read and write files through the Randomaccessfile object
When you produce an object, you can indicate the nature of the file you want to open: R, read only; W, write only; RW readable and writable
You can skip directly to the location specified in the file
Most of the functionality (not all) of Randomaccessfile is replaced by the NIO storage mapping file

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.