Java basics I / O flow usage details

The concept of "flow" comes from pipes in UNIX (pipe). In UNIX, a pipe is an uninterrupted byte stream, which is used to realize the communication between programs or processes, or read and write peripherals, external files, etc. it shields the details of data processing in the actual I / O device. A stream must be the active end and destination end. They can be some areas of computer memory, disk files, or even inte A URL on rnet. The direction of flow is important. According to the direction of flow, flow can be divided into two categories: input flow and output flow. In fact, I / O is intended for memory. In fact, the source end and destination end of a stream can be simply regarded as producers and consumers of bytes. For an input stream, you don't have to care about its source end. Just simply read data from the stream, while for an output stream, you don't know its destination, but simply write data to the stream.

a. Stream: an ordered sequence of data. B byte stream: the smallest data unit in the data stream is byte. c. Character stream: the smallest data unit in a data stream is a character.

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

One type of stream reads or writes directly from a specified location (such as disk file or memory area). This type of stream is called node stream, and the other is called filter stream (wrapper stream)

Filter streams: some streams can receive bytes from files and elsewhere, while others can combine bytes into more useful data types. The construction method of transferring an existing stream to another stream, combining the two streams, and the combined stream is called filter stream. Filter input stream often takes other input streams as its input source, and then provides it to users in the form of new input stream after filtering or processing. Filter output stream is also similar. We rarely use a single class to create flow objects, but stack multiple objects to provide the desired function (i.e. decorator design pattern).

The common input and output streams of Java are actually inherited from four abstract classes:

Based on single byte InputStream, OutputStream class (for I / O in byte form) is a reader based on double byte Unicode code unit. Writer class (for I / O in character form) once the input stream is opened, the program can read data serially from the input stream. The process of reading / writing data from the input stream is generally as follows: open a stream channel -- > read / write information -- > close the stream channel.

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

(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 and character (input and output) streams. Bytes are mainly processed by (OutputStream / InputStream) series and characters are mainly processed by (reader / write) series

II Byte oriented input streams (InputStream Series). These classes can be connected to FileInputStream objects to provide useful interfaces:

Bytearrayinputstream: take a buffer in memory as an InputStream, use stringbufferinputstream (which has been discarded in Java 1.1): take a string object as an InputStream, and the underlying implementation uses StringBuffer FileInputStream: take a file as an InputStream, Pipedinputstream: it implements the concept of pipe, Sequenceinputstream is mainly used in threads (as a data source in multiple processes): multiple inputstreams are combined into one InputStream. Byte oriented output streams (OutputStream Series) can be connected with filteroutputstream objects to provide useful interfaces:

Bytearrayoutputstream: creates a buffer in memory and stores information in a buffer in memory. Buffer initialization size (optional) fileoutputstream: stores information in a file (file name, file, filedescriptor) pipedoutputstream: implements the concept of pipe, which is mainly used in threads (specifies the destination of data for multithreading)

III Corresponding (reader / writer) series:

Reader: corresponds to InputStream, adapter inputstreamreader writer: corresponds to OutputStream, The adapter is outputstreamwriter FileReader: corresponding to fileoutputstream filewriter: corresponding to fileourputstream stringreader: no corresponding class stringwriter: corresponding to bytearrayinputstream chararrayreader: corresponding to bytearrayoutputstream chararraywriter: corresponding to bytearrayoutputstream pipedreader: corresponding to pipedinputstream pipedwrite r: Corresponding to pipedoutputstream

IV Conversion between two unrestricted streams (using adapter classes)

Inputstreamreader and outputstreamreader: convert a byte oriented stream into a character oriented stream.

Inputstreamreader is a bridge between byte flow and character flow: it uses the specified charset to read bytes and decode them into characters. The character set it uses can be specified or explicitly given by name, or it can accept the platform's default character set

Outputstreamwriter is a bridge between character flow and byte stream: the characters to be written to the stream can be encoded 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

V Read data from InputStream through filterinputstream:

Datainputstream: reads basic type (int, char, long, etc.) data from the stream. Bufferedinputstream: uses a buffer, which can prevent the actual reading operation for each reading. Linenumberinputstream: records the number of lines in the input stream, and then calls getlinenumber() and setlinenumber (int) pushbackinputstream: rarely used, It is generally used for compiler development to write to OutputStream through filteroutputstream:

Dataioutputstream: basic type (int, char, long, etc.) data can be output to the stream according to the migration method. Bufferedoutputstream: buffer is used to avoid actual write operation every time data is sent. Printstream: format output is generated, in which dataoutputstream handles data storage and printstream handles display

Vi Change the behavior of the flow

Although bufferedoutputstream is a subclass of filteroutputstream, bufferedwriter is not a subclass of filterwriter (filterwriter is an abstract class without any subclasses)

There is no class corresponding to datainputstream. Use datainputstream BufferedReader: corresponding to bufferedinputstream linenumberreader: corresponding to linenumberinputstream pushbackreader: corresponding to pushbackinputstream bufferedwrite: corresponding to bufferedoutstream printwrite: corresponding to printstream unless you use BufferedReader instead when using readline()

VII Self independent class: RandomAccessFile

This class is applicable to files composed of records of known size, RandomAccessFile implements datainput and dataoutput interfaces (datainputstream and dataoutputstream also implement these two interfaces). This class is a completely independent class. It has essentially different behavior from other I / O types. It can move forward and backward in a file and derive directly from object.

You can read and write files through the 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 directly jump to the specified location in the file. Most (but not all) functions of RandomAccessFile are replaced by NiO storage mapping file

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>