Dark Horse programmer--java IO stream

Source: Internet
Author: User

In Java, the IO stream requires operating system resources, and the resources are freed after the use is complete.

The IO stream can be divided into byte stream and character stream depending on the type of data being manipulated.

The character stream is based on the byte stream.

The top-level base class for character streams is reader and writer

The top-level base class for byte streams is inputstream and OutputStream

Flow is used to manipulate data, the most common form of data is a file, the character stream for a text file, the byte stream for the binary file (slice file).

1.FileWriter is a subclass of writer that creates a new file if the source file does not exist after the FileWriter object is created; overwrite if the source file exists

FileWriter has an overloaded constructor that specifies whether to overwrite the original data or append data when writing to the file, append data if passed in true

Its write () method writes the data to the stream, and only after the flush () method is executed can the data be added to the file.

The close () method, which also executes the flush () method before closing the resource.

2.FileReader is a subclass of reader whose read () method reads one character at a time, returns 1 when the end is read, and the second reads to the read () method

Passes in an array of characters, returns the number of unique characters, returns 1 when the end is read

The buffers in the 3.IO stream are designed to improve the operational efficiency of the flow. When the buffer is closed, it also closes its buffered stream object

(1) BufferedWriter

Usage: As long as the flow object that will improve efficiency is passed as a parameter to the constructor of the buffer.

Also execute the flush () or close () method to get the data to the destination

It has a newlne () method, which is a line break that spans the operating system.

(2) BufferedReader

Used in a similar way to BufferedWriter

It has a readline () method that reads one row of data at a time, and returns null if read to the end

ReadLine () Principle: either read a line, get more than one character, and eventually a read on the hard disk, so the end use of the Read method is the one-time method. The ReadLine () method is read

"\ r \ n" is considered to have finished reading a line (under Windows System) and then return the data.

(3) Decoration mode is based on the original class to enhance its function,

In decorating mode, we can pass the parent class of the decorated class as a parameter into the construction method of the adornment class, then you can apply this adornment class to the system, which is also the advantage of Java polymorphism. The use of adornment mode reduces the relationship between classes compared to the comparison. Decoration class is because of the enhancement of existing objects, has the same functionality and has been the same, but provides a more powerful function, so the decorative and decorated classes are usually part of a system.

4. The two top-level base classes of the byte stream are: InputStream (read), OutputStream (write)

The write () method of OutputStream does not require the flush () data to be written to the destination.

This is a character buffered output stream that keeps track of line numbers and can be set and fetched by the class's Setlinenumber (int) and getlinenumber (int) methods, respectively.

5. Convert the character stream object into a byte stream object, using the conversion flow: InputStreamReader (a bridge of bytes leading to characters)

OutputStreamWriter (the character to the Byte Bridge), that is, read in the characters, write in bytes, only the conversion stream can specify the encoding table

6. Summarize the rules of IO operations: through two explicit to complete:
(1) Clear source and purpose
Source: Input stream: Inputstream,reader
Purpose: Output stream: Outputstream,writer
(2) Whether the amount of the specified operation is plain text
Yes: Character stream
Not: Byte stream
(3) When the system is clear, then explicitly to use that specific object
Differentiate by device
SOURCE Device: Memory (array stream), hard disk (file stream), keyboard (system.in)
Destination Device: Memory (array stream), hard disk (file stream), console (System.out)

Dark Horse programmer--java IO stream

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.