The Java basics I and O flow USES detailed solutions

  • 2020-04-01 02:42:41
  • OfStack

The concept of "stream" comes from the concept of pipes in UNIX. In UNIX, a pipe is an uninterrupted stream of bytes used to communicate between programs or processes, or to read and write peripheral devices, external files, and so on, shielding the details of data processing in the actual I/O device.     A stream, must be the source and destination end, they can be some area of the computer memory, can be a disk file, can even be 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 types: input flow and output flow. The input/output is for memory. In fact, the source and destination ends of a stream can be simply viewed as producers and consumers of bytes. For an input stream, you don't have to care what the source end is, you simply read the data from the stream, and for an output stream, you don't know the destination end, but simply write the data into the stream.

A. Flow: an ordered sequence of data.
B byte stream: the smallest data unit in a data stream is a byte.
C. Character stream: the smallest data unit in a data stream is a character.

  A.   The classes in the java.io package correspond to two streams

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

Filter streams: some streams can receive bytes from files and elsewhere, and others can combine bytes into more useful data types. A construction method that passes an existing stream to another stream and combines the two streams. The combined stream is called a filter stream. Filter input stream usually takes other input stream as its input source, after filtering or processing, it is provided to the user in the form of a new input stream, and the filter output stream is similar. We rarely use a single class to create a flow object, but instead stack multiple objects to provide the desired functionality (that is, the decorator design pattern).

The commonly used input and output streams of Java are actually inherited from four abstract classes, which are:

Single byte based InputStream,OutputStream class (byte oriented I/O)
Reader, Writer class based on double byte Unicode code unit (character oriented I/O)
Once the input stream is opened, the program can read the data serially from the input stream. The process of reading/writing data from the input stream is generally as follows: open a channel --> Read/write information --> Close the circulation channel.

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

(a) System. GetProperty (" file. The encoding ");

(b) Charset cs = Charset. DefaultCharset ();

All InputStream, OutputStream can be divided into byte (input, output) stream, character (input, output) stream, byte processing is mainly (OutputStream/InputStream) series, processing characters, mainly is (Reader/Write) series

Byte - oriented InputStream, which can be connected to the FileInputStream object to provide a useful interface:

ByteArrayInputStream: USES a buffer in memory as an InputStream
StringBufferInputStream(deprecated in java1.1) : a String object is treated as an InputStream, and the underlying implementation USES StringBuffer
FileInputStream: reads a file as an InputStream (filename, file, FileDescriptor object)
PipedInputStream: implements the concept of pipe, primarily used in threads (as a data source in multiple processes)
SequenceInputStream: merge multiple inputstreams into one InputStream
A byte-oriented OutputStream that can be connected to a FilterOutputStream object to provide a useful interface:

ByteArrayOutputStream: creates a buffer in memory, stores the information in a buffer in memory, and initializes the buffer size (optional)
  FileOutputStream: puts information in a file (file name, file, FileDescriptor)
PipedOutputStream: implements the concept of pipe and is primarily used in threads (specifying the destination of data for multiple threads)

3. The corresponding Reader/Writer series :

Reader:                                 Corresponding to the InputStream, the adapter InputStreamReader
Writer:                                 In contrast to OutputStream, the adapter is OutputStreamWriter
FileReader:                           Corresponds to a FileOutputStream
  FileWriter:                         Corresponds to the FileOurputStream
  StringReader:                     There is no corresponding class
StringWriter:                     Corresponds to the ByteArrayInputStream
CharArrayReader:               Corresponds to the ByteArrayOutputStream
  CharArrayWriter:               Corresponds to the ByteArrayOutputStream
  PipedReader:                       Corresponding to the PipedInputStream
  PipedWriter:                       Corresponds to the PipedOutputStream

Four. Conversion between two types of undirected streams ( Use adapter class)

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

The InputStreamReader is the bridge between the byte stream and the character stream: it reads the byte using the specified charset and decodes it into a character. The character set it USES can be specified by name or given explicitly, or it can accept the platform's default character set

OutputStreamWriter is the bridge between the character stream and the byte stream: characters that are to be written into 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's default character set is accepted

five . Read data from InputStream via FilterInputStream :

DataInputStream: reads data of basic types (int, char, long, etc.) from the stream.
BufferedInputStream: use the buffer to prevent you from having to actually read each read
LineNumberInputStream: records the number of lines in the input stream, and then calls getLineNumber() and setLineNumber(int)
PushbackInputStream: rarely used, usually for compiler development
Write to the OutputStream through the FilterOutputStream:

DataIOutputStream: you can export data of basic types (int, char, long, etc.) to the stream in the migration mode.
  BufferedOutputStream: use the buffer to avoid the actual write operation every time the data is sent
  PrintStream: produces formatted output, where the DataOutputStream handles the storage of the data and the PrintStream handles the display

Six. Change the behavior of the flow

Although BufferedOutputStream is a subclass of FilterOutputStream, BufferedWriter is not a subclass of FilterWriter.

There is no class corresponding to the DataInputStream. Unless you use a BufferedReader instead of readLine(), use a DataInputStream
BufferedReader: corresponds to a BufferedInputStream
  LineNumberReader: corresponds to LineNumberInputStream
PushBackReader: corresponds to PushbackInputStream
BufferedWrite: corresponds to BufferedOutStream
PrintWrite: corresponds to PrintStream

Seven. Self - independent class :RandomAccessFile

This class is suitable for the file with the size of known record, RandomAccessFile in addition to implement DataInput and DataOutput interface (a DataInputStream and DataOutputStream also realized the two interfaces), the class is a completely independent classes, it has essentially different behavior and other I/O type, can move forward and backward within a file, is derived from the Object directly.

RandomAccessFile object can be used to complete the file read and write operation
When an object is generated, it indicates the nature of the file to be opened: r, read only; W, just write; Rw read/write
You can jump directly to the location specified in the file
Most (but not all) of the functions of RandomAccessFile are replaced by nio storage mapping files


Related articles: