Chapter 12 basic concepts of data stream 12.1 structured stream

Source: Internet
Author: User

Chapter 2 Java stream and file operations

12.1 basic concepts of data stream

 

The data source is provided, and the data sink is used to receive the data. Both are called data sources.

Data sources are diverse. For more effective data input and output operations, Java abstracts data transmission between different data sources and programs into "streams ", to achieve relatively unified and simple input and output operations.

Method 1: Based on the flow direction

Input stream and output stream

Method 2: Data Sources associated with data

Node stream: directly connects to the I/O Stream of the data source and reads/writes data from/to a specific place.

Filereader Fr = new filereader(includemo.txt ");

Stream processing: For an existing stream connection and encapsulation, the encapsulated stream function is called to implement enhanced data read/write functions. The processing stream is not directly connected to the data source.

Bufferedreader BR = new bufferedreader (New filereader(includemo.txt "));

Partitioning Method 3: data is divided by the size of particles transmitted.

1. byte stream: data transmission in bytes

2. bytes stream: data is transmitted in characters (when a byte stream is used to read one or more bytes, a specific encoding table is checked first, and then the retrieved characters are returned)

Byte stream can process all types of data

Character stream can only process character data

Preferred text stream

12.5 streams

The reader/writer class is a class that contains all input/output streams of object-oriented characters. It is declared as Java. Io.

In the IO system, the suffix of the subclass name is the parent class name, And the prefix is the name that reflects the subclass function.

Public int read (): Read a character and return the character to be read. If the end of the stream is read,-1 is returned.

Filereader Fr = new filereader ("E: \ A \ demo.txt ");

Int n = Fr. Read (); // read a character. The read character is output as the return value.

While (n! =-1) {// stop reading at the end

System. Out. Print (char) N );

N = Fr. Read ();

}

System. Out. println ();

Public int read (char [] cbuf): stores the read characters in the specified array, and returns the actual number of read characters. If the end of the stream is read,-1 is returned.

Public abstract int read (char [] cbuf, int off, int Len): stores the read characters into the specified position (off) of the array, and reads a maximum of Len characters each time, returns the number of characters actually read. If the end of the stream is read,-1 is returned.

Close (): the function of the window system is used to read characters. after use, resources are released.

Filewriter fw = new filewriter ("demo.txt ");

FW. Write ("Hello word"); // complete the write operation

FW. Flush (); // refresh the data in the data source to the file

FW. Write ("Java"); // add content to the file

FW. Close (); // close

Public void write (char []): writes the characters in the array in sequence.

Public abstract void write (char [] bcbuf, int off, int Len): Write the Len characters starting with subscript off in the array.

Public void write (int c): Write a character to the stream.

Public void write (string): Write a string to the stream.

Public abstract void flush (): refreshes the stream, refreshes the data in the stream to the destination, and the stream still exists.

Public abstreact void close (): Close the resource. Before closing, flush is called to refresh the data in the stream to the destination, and the stream is closed.

 

 

 

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.