Java notes (day23-day26)

 IO streams
1, copy a text file.

    1, clear systems:
        Source: InputStream, Reader
        purpose: OutputStream, Writer
    2, clear data:
        Source: plain text it? Reader is a
        purpose; it is plain text? It is Writer
    3, specifically equipment:
        source: a file on the hard disk. FileReader
        purpose: a file on the hard disk. FileWriter
        FileReader fr = new new FileReader ( "a.txt");
        FileWriter fw = new new FileWriter ( "b.txt");
    4, need extra features?
        Required, efficient, using Buffer
        the BufferedReader BUFR the BufferedReader new new = (the FileReader new new ( "a.txt"));
        BufferedWriter, BUFW BufferedWriter, new new = (new new FileWriter ( "b.txt"));
        
        
2, read the keyboard input, the data storage to a file.
    1, clear systems:
        Source: InputStream, Reader
        Objective: OutputStream, Writer
    2, clear data:
        Source: plain text it? Reader is a
        purpose; it is plain text? It is Writer
    3, specifically equipment:
        Source: keyboard, System.in
        purpose: hard disk, FileWriter
        InputStream in = System.in;
        FileWriter fw = new new FileWriter ( "a.txt");
    4, need extra features?
        Need, because clearly the source of system Reader. But the source device is System.in.
        Therefore, in order to facilitate the operation of the text data, converted into the source character stream. We need to convert the stream. InputStreamReader
        InputStreamReader ISR = new new InputStreamReader (System.in);
        FileWriter fw = new new FileWriter ( "a.txt");
        the need for efficient not need?. Buffer
        the BufferedReader BUFR the BufferedReader new new = (the InputStreamReader new new (the System.in));
        BufferedWriter, BUFW BufferedWriter, new new = (new new FileWriter ( "a.txt"));
        

3, to read a text file, the data show on the console.
        1, clear systems:
            Source: InputStream, Reader
            purpose: OutputStream, Writer
        2, clear data:
            Source: plain text it? Reader is a
            purpose; it is plain text? It is Writer
        3, specifically equipment:
            Source: hard disk file, FileReader.
            Objective: console: System.out.
            FileReader fr = new new FileReader ( "a.txt");
            OutputStream OUT = System.out;
        4, need additional functionality?
            Because the source is text data, determine Writer system. Therefore, in order to facilitate operation of the character data,
            required character stream, but it is also an object of the output byte stream.
            Requires a conversion stream, the OutputStreamWriter
            the FileReader the FileReader fr = new new ( "a.txt");
            the OutputStreamWriter the OutputStreamWriter new new OSW = (the System.out);
            
            Need for efficient it? need.
            = New new BUFR the BufferedReader the BufferedReader (new new the FileReader ( "a.txt"));
            BufferedWriter, BUFW BufferedWriter, new new = (the OutputStreamWriter new new (the System.out));
            


. 4, read the keyboard input, the data presented on the console.
        1, clear systems:
            Source: InputStream, Reader
            purpose: OutputStream, Writer
        2, clear data:
            Source: plain text it? Reader is a
            purpose; it is plain text? Is Writer
        3, specifically equipment:
            Source: Keyboard: System.in
            Objective: Console: System.out
            InputStream in = System.in;
            OutputStream OUT = System.out;
        4, need extra features?
            Because the data is processed text data, and determine the character stream system.
            To facilitate operation of the character data may be the source and destination are transformed into the character stream. Use conversion flow.
            To improve efficiency, using Buffer
            the BufferedReader BUFR the BufferedReader new new = (the InputStreamReader new new (Systme.in));
            ; BufferedWriter, BUFW BufferedWriter, new new = (the OutputStreamWriter new new (the System.out))
            
            


. 5, reads a text file, the file according to the specified coding table UTF-8 is stored, saved to another file.
        1, clear systems:
            Source: InputStream, Reader
            purpose: OutputStream, Writer
        2, clear data:
            Source: plain text it? Reader is a
            purpose; it is plain text? Is Writer
    
        . 3, the device clear:
            Source: Hard: FileReader.
            Objective: Hard: FileWriter
            
            the FileReader the FileReader fr = new new ( "a.txt");
            FileWriter FileWriter new new FW = ( "b.txt");
        . 4, additional features:
            Note : Although the purpose of a file, but need to specify the encoding table.
            The FileWriter direct manipulation of the text file itself is built of the local default code table. Not clearly specify the code table.
            Then you need conversion. The OutputStreamWriter, and this converted stream need to receive a output stream of bytes, and the
            object corresponds to a file. In this case it is the operation target using the file byte stream in the output stream. . FileOutputStream
            FileReader fr = new new FileReader ( "a.txt");            
            OutputStreamWriter OSW = new new OutputStreamWriter (new new FileOutputStream ( "b.txt"), "UTF-8");
            
            the need for efficient it?
            = New new BUFR the BufferedReader the BufferedReader (new new the FileReader ( "a.txt"));
            BufferedWriter, BUFW =
                new new BufferedWriter, (the OutputStreamWriter new new (new new a FileOutputStream ( "b.txt"), "UTF-. 8"));
    
        
            
So far, 10 stream object master key.
A stream of characters:
FileReader
FileWriter

BufferedReader
BufferedWriter

The InputStreamReader
OutputStreamWrier
byte stream:

the FileInputStream
a FileOutputStream

BufferedInputStream
BufferedOutputStream The

File categories:
    files and folders for encapsulating as an object.
    
    1 created.
        boolean createNewFile (): if the file does not exist, it is created, if it already exists, not created. It will not be the same coverage as the output stream.        
        mkdir boolean ();
        boolean mkdirs ();
    2, delete.
        Boolean Delete ();
        void deleteOnExit ();
        
    . 3, obtain:
        String getAbsolutePath ();
        String getPath ();
        String getParent ();
        String getName ();
        Long length ();
        Long the lastModified ();
        
    
    . 4, is determined:
        Boolean EXISTS ();
        isFile Boolean ();
        Boolean the isDirectory ();

other functions in the stream object IO:

1, print streams:
    PrintStream: byte print stream.
        Features:
        1, receiving File object constructor, the string path, the output byte stream. It means that printing can have many purposes.
        2, the object includes a unique method of printing method print println, any type of data can be printed.
        3, a unique method may remain as print any type of data presentation, of the output data to the destination.
            For OutputStream parent class write, it is the least significant byte of data written out.
            
    PrintWriter: character print stream.
        Features:
        1, the operation when the data is the character, it is possible to select the PrintWriter, for convenient than PrintStream.
        2, it may receive a File object constructor, the string path, the output byte stream, the output stream of characters.
        3, the constructor, if the parameter is an output stream, it can be done by specifying other parameters automatically refresh true, the true effective println method.

When to use?
When the data as is necessary to ensure the performance, you can use the Print method to print stream to complete, so more convenient.
Guarantee the principle intact nature: in fact, turn into a string of data, during the write operation.



SequenceInputStream:
    Features:
    1, and a plurality of streams of bytes read, and read into a stream, a plurality of sources into a combined source, easy to operate.
    2, the interface may be required by the enumeration Collections.enumeration (Collection);
    


the ObjectInputStream ObjectOutputStream and

serialization and deserialization of the objects.

readObject writeObject

Serializable marker interface

Keywords: transient


RandomAccessFile:
    Features:
    1, can be read, but also write.
    2, maintains a large internal byte array, the array is completed through the operation of reading and writing.
    3, by obtaining the pointer position getFilePointer method, the position of the pointer may be provided by the seek method.
    4, contents of the object should encapsulate the input stream of bytes and the output byte stream.
    5, the object can only operate files.
    
    It can be from any location on the array by a method for operating a read and write pointer seek
    complete modification of data.
    But beware: data must be regular.

Pipe flow: Stream object and requires a combination of multi-threading techniques.
The PipedOutputStream
the PipedInputStream

Basic Operational data type value.
    DataInputStream
    DataOutputStream

设备是内存的流对象。
ByteArrayInputStream ByteArrayOutputStream
CharArrayReader  CharArrayWriter

IO流体系:

字符流:
Reader
    |--BufferedReader:
        |--LineNumberReader
    |--CharArrayReader
    |--StringReader
    |--InputStreamReaer
        |--FileReader


Writer
    |--BufferedWriter
    |--CharArrayWriter
    |--StringWriter
    |--OutputStreamWriter
        |--FileWriter
    |--PrintWriter

字节流:
InputStream
    |--FileInputStream:
    |--FilterInputStream
        |--BufferedInputStream
        |--DataInputStream
    |--ByteArrayInputStream
    |--ObjectInputStream
    |--SequenceInputStream
    |--PipedInputStream

OutputStream
    |--FileOutputStream
    |--FilterOutputStream
        |--BufferedOutputStream
        |--DataOutputStream
    |--ByteArrayOutputStream
    |--ObjectOutputStream
    |--PipedOutputStream
    |--PrintStream

Guess you like

Origin www.cnblogs.com/wanggang521/p/11032726.html