Java Basics IO Stream (i)

Source: Internet
Author: User
Tags int size

Java Base IO Stream (i) byte stream

  IO flow system is too large, involving a variety of flow objects, I think it is necessary to summarize.

What is an IO stream, IO represents input, Output, and the flow is an abstraction of the data transfer from the original source to the target medium. Typical data sources and target vectors include disk, network, memory, and so on.

  Classification of IO streams:

    Divided by flow: input stream and output stream (local memory as reference)

By Process data unit: character stream and Byte stream

Depending on whether it is connected to a specific location (disk, network, Memory): node flow and processing flow

Node Flow: You can read and write data from or to a specific place (node).

Processing flow: Is the connection and encapsulation of an existing stream, which implements the data read and write through the functional invocation of the encapsulated stream.

is commonly used in the IO stream, it should be noted that the arrow pointing does not represent a direct inheritance relationship. The flow that appears in this diagram is also described later in this article.

(i) Byte stream

  According to the information, the top-level abstract classes corresponding to the byte stream are InputStream and OutputStream, and the universal flow at the byte stream can handle any type of data.

The InputStream provides a basic way to read data:

int read (): reads one byte at a time, the return value is the read byte, and if the return value is-1, it reads to the end.

int read (byte[] b): reads one byte array at a time, and the return value is the valid length read.

int read (byte[] b, int offset, int len): reads one part of a byte array at a time (less), returns the total number of bytes read into the buffer, and returns 1 if read to the end

Its sub-class is based on three reading functions of the implementation and function of the expansion.

OutputStream provides a basic way to write data:

void write (int b): writes one byte at a time, the parameter is an int variable, and if B exceeds the range of byte, it is processed in a high-truncation manner.

void Write (byte[] bys): writes one byte array at a time.

void Write (byte[] bys, int offset, int len): writes one part of a byte array at a time.

Its subclass is on the basis of these three write functions to carry out their own implementation and function expansion.

  1. FileInputStream and FileOutputStream

 As you can see from the class name, this pair of input and output streams targets the data source and target medium for the file system.

FileInputStream:

There are two methods of construction:

FileInputStream (String fileName) and FileInputStream (file file).

FileInputStream only implements the basic reading method provided by InputStream, and does not have a special function.

FileOutputStream:

Consists of four construction methods:

FileOutputStream (String fileName) and FileOutputStream (file file)

FileOutputStream (String Filename,boolean append) and FileOutputStream (file file, Boolean append): Indicates whether the Write method is appended at the end of the target medium.

FileOutputStream, the parent class feature is not extended.

2. Bufferedinputstream and Bufferedoutputstream

The byte stream with its own buffer, as can be seen from his construction method, is a processing stream that internally encapsulates a InputStream or OutputStream object.

Buffeedinputstream Construction Method:

Bufferedinputstream (inputstream in, int size): Specifies the size of the byte stream and buffer within which it is encapsulated.

Bufferedinputstream (inputstream in, int size): Specifies the internal encapsulated byte stream, and the buffer size takes the default value.

Private Static int Default_buffer_size = 8192; // Default buffer 8*1024 protected volatile byte buf[]; // where the data is actually stored

The Bufferedoutputstream construction parameter is the same as Bufferedinputstream, which specifies the internal encapsulated output stream and buffer size.

Bufferedinputstream and Bufferedoutputstream both encapsulate the node stream, internally maintaining a buffer, the specific data read and write through the incoming node stream, in the read and write function is not extended.

3.DataInputStream and DataOutputStream

Consider the question of manipulating data of the basic data type, what if you don't use the data stream? The answer is high-level, resulting in loss of precision. The mission of data streams (DataInputStream and DataOutputStream) is to complete the reading and writing of basic data types.

It is also a process flow, which encapsulates the flow of nodes internally, as can be seen from its construction method:

DataInputStream (InputStream in), which provides read operations for the basic data type: ReadInt (); ReadChar (); ReadByte (); Readboolean (); readdouble (), etc.

DataOutputStream (OutputStream out): A write operation that contains the corresponding base data type.

Its internal implementation principle of reading and writing various data is also through a byte of reading, and then through the bits shift operation.

One way to pay special attention:

writeUTF (): Using a modified version of the UTF-8 encoding: The method first writes two bytes to represent the length of the string to be written, followed by the byte representation of the string to be written, each of which has a byte range. Thus the total length is 3 times times the length of the string and the length of the string. The readUTF () is used to read the writeUTF write String.

  4. Bytearrayinputstream and Bytearrayoutputstream

  These two streams are used to temporarily process the data.

The key to understanding Bytearrayoutputstream is to understand its data sources and target vectors. The data source is easy to understand, that is, by writing (int), write (byte[]), write (byte[], int, int) method written data, the target medium is a byte array in memory, can be automatically expanded, the size of the memory byte array can be set through the construction method, You can also use the default size.

protected byte buf[]; // the byte array that actually stored the data protected int count; // the effective length of the byte array  Public Bytearrayoutputstream () {  this (+);   Default length is+}

  The Bytearrayinputputstream data source is a byte array passed through the constructor, and there is also a byte array inside to store the data read from.

protected byte   public bytearrayinputstream (byte  buf[]) {        this. buf = buf;          this. pos = 0;         this. Count = buf.length;    }

5.PrintStream

  PrintStream is used to decorate other output streams to add functionality to other output streams, allowing them to print out various data value representations. Unlike other output streams,PrintStream never throws IOException, it generates errors that are caught by its own function and sets an error mark, and the user can return an error token by CheckError (). See if IOException is generated. PrintStream provides automatic flush and character set settings, and the data written will immediately call the flush () function. PrintStreamall characters that are printed are converted to bytes using the platform's default character encoding, or you can set the character sets by constructing the method.

Java Basics IO Stream (i)

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.