A summary of the knowledge points of "learning diary" about Java IO

Source: Internet
Author: User
Tags flush inheritance readline
Recently in the Bi Xiangdong Teacher's Java basic video IO part of the knowledge of the explanation. Because IO this piece of knowledge is very important, and small knowledge dot many, method also more, tonight make a simple summary for later review.

IO Overview The method of the stream of words the method of byte throttling the introduction of the decorative design pattern buffer

IO Overview

Io in Java is a very important concept and knowledge point, according to the flow into the input and output streams, according to the operation of the data divided into bytes * * and * * Character stream * *, as the name implies, the word stream in bytes, character streams in characters as a unit. The Java.io package contains various classes and their interfaces about Io, where inputstream and reader are the parent of all input streams, and OutputStream and writer are the parent classes of all output streams. *inputstream* and *outputstream* belong to the byte stream, and *reader* and *writer* belong to character streams.
methods of character streams
The character stream is manipulated by characters, and it is convenient to manipulate the text file to avoid coding.
common classes for character streams
Classification input Stream output Stream
Abstract base class Reader Writer
accessing files FileReader FileWriter
Buffer stream BufferedReader BufferedWriter
The construction method of FileWriter
FileWriter (String s) Create file, S: File address and filename
FileWriter (String S,boolean t) Continue to write the file, s: File address and filename, T:true continued. False new.
Common methods of writer
Close (); Close this stream, but first refresh it
Flush (); Refresh the buffer for this stream
Write (int c/char[] cbuf/string arg/char[] cbuf, int off, int len) Writes a single character, writes a character array, writes a string, writes a part of a character array, or writes part of a string
Common methods of Reader
Close (); Close this stream
Read (void/char[] cbuf/char[] cbuf, int off, int len) Reads a single character, reads a character into an array, and reads a character into part of an array
method of Word throttling
Byte streams are used to manipulate data in bytes, and can be processed in a wide variety of files.
common classes for byte streams
input stream output stream
abstract base class InputStream outputstream
Access Files FileInputStream FileOutputStream
buffer Flow bufferedinputstream bufferedoutputstream
Common methods of FileOutputStream
Close () Closes this output stream and releases all system resources related to this stream
Flush () Refreshes this output stream and forces all buffered output bytes to be written to the stream
Write (byte[] b/byte[] B, int off, int len/int b) Writes B.length bytes to this output stream, writes Len bytes from offset off in the specified byte array to this output stream, writes the specified byte to this output stream
Available () Returns the estimated remaining bytes of the next method that is invoked on this input stream that can be read (or skipped) from this input stream without blocking
Close () Closes this input stream and releases all system resources associated with this stream
Common methods of FileInputStream
Read (void/byte[] b/byte[] B, int off, int len) Reads the next data byte from this input stream, reads the byte.length byte data from the input stream into a byte array, and reads the Len byte data from the input stream into a byte array

Decorative design mode

When you want to enhance the functionality of an existing object, you can define the class, pass in existing objects, build on existing functionality, and provide enhanced functionality. Then the custom class is called the decoration class .
The decoration class usually receives the decorated object by constructing the method . and provides stronger functionality based on the functionality of the objects being decorated. decoration mode is more flexible than inheritance , avoids the bloated inheritance system, and reduces the relationship between classes.
The decorative class has the same functionality as the existing object, but it provides a stronger function. So the decoration class and the decorated class are usually all belong to one system.

ReadLine principle is the use of decorative design mode
The method is to use the FileReader read method. Creates a StringBuilder and then reads a character with read continuously and stores it in the string cache, and when read to \ r, does not save \ r, when read \ n, indicates that the line ends, and returns the string cache to its string.

The code is as follows:

Class Myline 
{
    private Reader R;
    Myline (Reader R)
    {
        THIS.R = R;
    }
    Public String Myreadline () throws IOException
    {
        StringBuilder sb = new StringBuilder ();
        int ch = 0;
        while ((Ch=r.read ())!=-1)
        {
            if (ch== ' \ R ')
                continue;
            if (ch== ' \ n ') return
                sb.tostring ();
            else
                Sb.append ((char) ch);
        if (Sb.length ()!=0) return
            sb.tostring ();
        return null;
    }
    public void Myclose () throws IOException
    {
        r.close ();
    }
}
Introduction to Buffers

Buffer classes for character streams:BufferedReader and bufferedwriter
Buffer classes for byte streams:bufferedinputstream and bufferedoutputstream

The

Buffer is a performance optimization for IO. The BufferedReader class and the BufferedWriter class make a buffer wrapper over the character stream reader/write. The
buffer appears to increase the operational efficiency of the stream. So before you create a buffer, you must first have a stream object. The created stream object is then passed as a parameter to the buffered constructor.
Whenever you use a buffer, you must remember to refresh. Closes the buffer, which is the stream object in the closed buffer. Therefore, it is not necessary to close the stream object separately. A common method of newline () ( character stream-specific ) writes a row separator. Flush (); Refreshes the cache for this stream. Read (); reads a single character/byte/reads a specific character or byte. ReadLine ();( character streams unique ) Reads a line of text and returns as a string. When NULL is returned, the end of the file is reached. This method returns the data between the carriage return, excluding the carriage return, so a return character is required, plus a newline (). Write (); writes the specified byte/byte array (or part)/single character/string (or part)/character array (or part).

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.