Knowledge of some IO streams

Source: Internet
Author: User
Tags processing text

IO stream:

Input stream:
Output stream:

BYTE stream:
Character stream: An object that appears handy for processing text data.
In fact, the internal use of these objects is still a byte stream (because the text is ultimately byte data)
However, the byte stream reads the corresponding number of bytes, and there is no direct manipulation of those bytes.
Instead, we checked the specified (native default) encoding table and got the corresponding text.

Simply put: Character stream is: Byte stream + encoding table.

-----------------------

Buffers: improving efficiency and improving who's efficiency? Improve the efficiency of the flow of operational data.
So you must have a stream before you create the buffer.
The basic idea of a buffer is to define the container to store the data temporarily.
For buffer objects, this container is actually encapsulated and provides more efficient methods of operation.

Buffers can improve the operational efficiency of the flow.

In fact, it is done with a design idea. Design pattern: Decorative design mode.

Writer
|--textwriter
|--mediawriter
Now you want to enhance the functionality of the objects in the system. The most common means of enhancement are buffers.
Writes the data to the buffer first, and then writes the data in the buffer once to the destination.

According to the basic idea of the previous study, it is to overwrite the writing method in the object.
Produces an existing object subclass, the replication write method. Do not write to the destination, but write to the buffer.

So this system will become like this.
Writer
|--textwriter write: Go to destination
|--buffertextwriter write: Writes to buffer
|--mediawriter
|--buffermediawriter

Want to write some other data. Just the CBD class. DataWriter, in order to improve its efficiency, it is also necessary to create subclasses of the class. Bufferdatawriter
Writer
|--textwriter write: Go to destination
|--buffertextwriter write: Writes to buffer
|--mediawriter
|--buffermediawriter
|--datawriter
|--bufferdatawriter

Find the system quite troublesome. There is an efficient subclass for each subclass to be produced.
And this write-efficient subclass uses the same functional principle, which is the buffer principle. No matter what the data is.
are more efficient through temporary storage of buffers.
Then, the system can be optimized because there is no need for each object to have subclasses with the same functionality.

Which object wants to be more efficient, just let the buffer operate on it. It is also said that the buffer is encapsulated individually into an object.

It appears to improve the efficiency of the object. So there must be an object that needs to be more efficient when it is created
Class Bufferwriter
{
[];
BufferedWriter (Writer W)
{

}
/*
Bufferwriter (TextWriter W)
{

}
BufferedWriter (Mediawriter W)
{

}
*/
}
The advent of Bufferwriter enhanced the Write method in writer.
But after the enhancement, the Bufferwriter provides the write method externally. It's just that it's efficient.
So the essence of writing has not changed, then Bufferwriter is also a member of writer.
So the system will become like this.
Writer
|--textwriter
|--mediawriter
|--bufferwriter
|--datawriter
Bufferwriter appeared to avoid the bloated inheritance system relationship, more flexible than inheritance.
This approach is easier to solve if it is to enhance the functionality.
So this optimization, summed up, a name: decorative design patterns.

The decoration class and the adornment class certainly belong to the same individual department.


Since the bufferedreader of the origin of the clear.
We can also complete the creation of the buffer independently

Principle
1, the Read method of the stream is used to fetch a batch of data from the source stored in the array of buffers.
2, the number of stored elements is recorded by the counter.
3, get the elements in the array (fetch data from the buffer) by the array's Corner label.
4, the pointer will continue to increment, when added to the array length, will be 0. The counter is self-decreasing, and when it is reduced to 0 o'clock, a batch of data is taken from the source into the buffer.

Content to complement:
Mybufferedreader
LineNumberReader: You can define line numbers.
---------
Character Stream:
FileReader
FileWriter

BufferedReader
BufferedWriter

BYTE stream:
InputStream OutputStream.

The byte stream object that operates the file.
FileOutputStream
FileInputStream
Bufferedoutputstream
Bufferedinputstream


The conversion action between a character stream and a byte stream.

----------

Conversion Flow:

InputStreamReader ISR = new InputStreamReader (New FileInputStream ("A.txt"));
InputStreamReader ISR = new InputStreamReader (New FileInputStream ("A.txt"), "GBK");
FileReader FR = new FileReader ("A.txt");


FileWriter FW = new FileWriter ("B.txt");
OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("B.txt"));
OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("B.txt"), "GBK");

Conversion stream: Byte stream + encoding table.
Subclass of Conversion stream: Filereader,filewriter: Byte stream + local default Code table (GBK).

If you manipulate the text file, use the local default encoding table to complete the encoding. You can use FileReader, or filewriter. Because it's easy to write.
If the text file for the operation needs to be decoded using the specified encoding table, the conversion flow must be used to complete.


-----------------------------


The operation rule of Io flow is summarized as follows:

1, clear system:
Data source: InputStream, Reader
Data exchange: Outputstream,writer

2, clear data: Because the data is divided into two kinds: byte, character.
Data source: Is it plain text data?
Yes: Reader
No: InputStream

Data sinks:
Yes: Writer
No: OutputStream
Here you can specify which system to use.
The rest is to make a clear use of which object in the system.

3, Clear equipment:
Data Source:
Keyboard: System.in
Hard drive: filexxx
Memory: Array.
Network: Socket Socket.getinputstream ();

Data sinks:
Console: System.out
Hard drive: filexxx
Memory: Array
Network: Socket Socket.getoutputstream ();

4, clear additional features:
1, need to convert? Yes, use the transform stream. InputStreamReader OutputStreamWriter
2, need to be efficient? Yes, use a buffer. Buffered
3, need other?

--------------------

1, copy a text file.

1, clear system:
Source: InputStream, Reader
Purpose: OutputStream, Writer
2, Clear data:
Source: Is it plain text? It's Reader.
Purpose; Is it plain text? It's Writer.
3, Clear equipment:
Source: A file on the hard disk. FileReader
Purpose: A file on the hard disk. FileWriter
FileReader FR = new FileReader ("A.txt");
FileWriter FW = new FileWriter ("B.txt");
4. Do you need extra functionality?
Required, efficient, using buffer
BufferedReader bufr = new BufferedReader (New FileReader ("A.txt"));
BufferedWriter BUFW = new BufferedWriter (New FileWriter ("B.txt"));


2, read the keyboard input, the data stored in a file.
1, clear system:
Source: InputStream, Reader
Purpose: OutputStream, Writer
2, Clear data:
Source: Is it plain text? It's Reader.
Purpose; Is it plain text? It's Writer.
3, Clear equipment:
Source: Keyboard, system.in
Purpose: Hard disk, FileWriter
InputStream in = system.in;
FileWriter FW = new FileWriter ("A.txt");
4. Do you need extra functionality?
Required because of the source-specific system when reader. But the source of the device is system.in.
Therefore, to facilitate the manipulation of text data, the source is converted into a character stream. The stream needs to be converted. InputStreamReader
InputStreamReader ISR = new InputStreamReader (system.in);
FileWriter FW = new FileWriter ("A.txt");
Need to be efficient? Buffer
BufferedReader bufr = new BufferedReader (new InputStreamReader (system.in));
BufferedWriter BUFW = new BufferedWriter (New FileWriter ("A.txt"));

3, read a text file and display the data on the console.
1, clear system:
Source: InputStream, Reader
Purpose: OutputStream, Writer
2, explicit data:
Source: Is it plain text? is Reader
purpose; Is it plain text? is Writer
3, clear device:
Source: Hard disk File, FileReader.
Purpose: Console: System.out.
FileReader fr = new FileReader ("A.txt");
OutputStream out = System.out;
4, need additional features?
because the source is text data, it is determined to be a writer system. Therefore, to facilitate the manipulation of character data, the
uses a character stream, but is intended to be a byte output stream. The
requires a conversion stream, OutputStreamWriter
FileReader fr = new FileReader ("A.txt");
OutputStreamWriter OSW = new OutputStreamWriter (System.out); Does

need to be efficient? Need.
BufferedReader bufr = new BufferedReader (New FileReader ("A.txt"));
BufferedWriter BUFW = new BufferedWriter (new OutputStreamWriter (System.out));


4, read the keyboard input and display the data on the console.
1, clear system:
Source: InputStream, Reader
Purpose: OutputStream, Writer
2, explicit data:
Source: Is it plain text? is Reader
purpose; Is it plain text? is Writer
3, explicit device:
Source: Keyboard: system.in
Purpose: Console: System.out
InputStream in = system.in;
OutputStream out = System.out;
4, do you need extra functionality?
because the processed data is text data and is also determined to be a character stream system.
to facilitate the manipulation of character data, you can turn both the source and the destination into a character stream. Use the transform stream.
to increase efficiency, use buffer
BufferedReader bufr =new bufferedreader (New InputStreamReader (systme.in));
BufferedWriter BUFW = new BufferedWriter (new OutputStreamWriter (System.out));


5, read a text file, the file according to the specified encoding table UTF-8 storage, save to another file.
1, clear system:
Source: InputStream, Reader
Purpose: OutputStream, Writer
2, Clear data:
Source: Is it plain text? It's Reader.
Purpose; Is it plain text? It's Writer.

3, Clear equipment:
Source: Hard drive: FileReader.
Purpose: Hard disk: FileWriter

FileReader FR = new FileReader ("A.txt");
FileWriter FW = new FileWriter ("B.txt");
4, Additional features:
Note: Although the destination is a file, you need to specify the encoding table.
The filewriter itself, which directly operates the text file itself, is built into the local default code table. The Code table cannot be specifically specified.
The conversion function is required. OutputStreamWriter, and this conversion stream needs to accept a byte output stream, and
The corresponding purpose is a file. The stream object of the action file in the byte output stream is then used. FileOutputStream.
FileReader FR = new FileReader ("A.txt");
OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("B.txt"), "UTF-8");

Do you need to be efficient?
BufferedReader bufr = new BufferedReader (New FileReader ("A.txt"));
BufferedWriter BUFW =
New BufferedWriter (New OutputStreamWriter (New FileOutputStream ("B.txt"), "UTF-8");



So far, 10 stream objects have been focused.
Character Stream:
FileReader
FileWriter

BufferedReader
BufferedWriter

InputStreamReader
Outputstreamwrier
BYTE stream:

FileInputStream
FileOutputStream

Bufferedinputstream
Bufferedoutputstream


--------------------------------
File class:
Used to encapsulate files and folders into objects.

1, create.
Boolean createnewfile (): If the file does not exist, it is created and is not created if it already exists. Will not be overwritten like an output stream.
Boolean mkdir ();
Boolean mkdirs ();
2, delete.
Boolean delete ();
void Deleteonexit ();

3, Get:
String GetAbsolutePath ();
String GetPath ();
String getParent ();
String GetName ();
Long length ();
Long LastModified ();


4, Judge:
Boolean exists ();
Boolean isfile ();
Boolean isdirectory ();


5,

----------------------


Other feature Stream objects in IO:

1, Print stream:
PrintStream: Byte print stream.
Characteristics:
1, the constructor receives a file object, a string path, and a byte output stream. means there can be a lot of printing purposes.
2, the object has a unique method of printing the method print println, you can print any type of data.
3, the unique print method can maintain the original nature of any type of data representation, and output the data to the destination.
For write in the OutputStream parent class, the lowest byte of the data is written out.

PrintWriter: Character print stream.
Characteristics:
1, when the operation of the data is a character, you can choose PrintWriter, more convenient than PrintStream.
2, its constructor can receive a file object, a string path, a byte output stream, a character output stream.
3, in the constructor, if the argument is an output stream, you can complete the automatic refresh by specifying another parameter, true, which is valid for the Println method.

When do you use it?
When you need to ensure that the data performance is the same, you can use the print flow printing method to complete, it is more convenient.
The principle of guaranteeing the original: In fact, the data into a string, in the write operation.

Sequenceinputstream:
Characteristics:
1, the multiple bytes read stream and into a read stream, the multiple sources are combined into a single source, easy to operate.
2, the required enumeration interface can be passed collections.enumeration (collection);


ObjectInputStream and ObjectOutputStream

The serialization and deserialization of the object.

WriteObject ReadObject

Serializable Tag Interface

Keyword: transient


Randomaccessfile:
Characteristics:
1, can read and write.
2, the internal maintenance of a large byte array, through the operation of the array to complete the read and write.
3, the position of the pointer is obtained through the Getfilepointer method, and the position of the pointer can also be set by the Seek method.
4, the object's contents should encapsulate the byte input stream and the byte output stream.
5, the object can only manipulate files.

You can read and write from anywhere in the array by using the Seek method to manipulate the pointer.
You can complete the modification of the data.
However, be aware that data must be regular.


Pipeline flow: A stream object that needs to be combined with multithreaded technology.
PipedOutputStream
PipedInputStream


The object that is used to manipulate the base data type value.
DataInputStream
DataOutputStream

A device is a stream object of memory.
Bytearrayinputstream Bytearrayoutputstream
CharArrayReader Chararraywriter
--------------------

IO Flow system:

Character Stream:
Reader
|--bufferedreader:
|--linenumberreader
|--chararrayreader
|--stringreader
|--inputstreamreaer
|--filereader


Writer
|--bufferedwriter
|--chararraywriter
|--stringwriter
|--outputstreamwriter
|--filewriter
|--printwriter

BYTE stream:
InputStream
|--fileinputstream:
|--filterinputstream
|--bufferedinputstream
|--datainputstream
|--bytearrayinputstream
|--objectinputstream
|--sequenceinputstream
|--pipedinputstream


OutputStream
|--fileoutputstream
|--filteroutputstream
|--bufferedoutputstream
|--dataoutputstream
|--bytearrayoutputstream
|--objectoutputstream
|--pipedoutputstream
|--printstream

Randomaccessfile:

Knowledge of some IO streams

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.