Summary of IO flow knowledge

Io style

File class

Relative path: Compared to a path, the specified path

Absolute path: the path of the file or file directory including the drive letter

The path separator is related to the system:

​ Windows and Dos systems use "\" by default

​ Unix and URL use "/" to indicate

Method of creating File

File(String, int)
File(String, File)
File(String)
File(String, String)
File(File, String)
File(URL)

IO style

Used to process data transfer between devices

Input: read external data (data from storage devices such as disks, CDs, etc.) to the program (in memory)

Output: output program (memory) data to storage files such as disks, CDs, etc.

Abstract base flow Byte stream Character stream
Input stream InputStream Reader
Output stream OutputStream Writer
Abstract base flow Node stream (file stream) Buffer stream (a type of processing stream)
InputStream FileInputStream BufferedInputStream(read (byte[] buffer))
OutputStream FileOutputStream BufferedOutputStream(write (byte[] buffer, 0, len))/flush()
Reader FileReader BufferedReader(read (char[] cbuffer)/readline())
Writer FileWriter BufferedWriter(write (char[] cbuffer, 0, len))/flush()
FileReader

read() returns a character read, or -1 if the file reaches the end.

Exception handling: In order to ensure that the stream resource can be closed, try-catch-finally is required.

The read file must exist, otherwise there will be FIleNotFoundException

FileWriter

For output operation, the corresponding File may not exist, and no exception will be reported.

​ If it does not exist, the file will be created automatically during the output process;

​ If the file exists, FileWriter(File, false)/FileWriter(File) will overwrite the original file . If FileWriter(File, true) is used , it will not

​ The original file will be overwritten but appended .

You cannot use character streams to process images, but use byte streams.

​ For text files, use character stream processing (.txt, .java, .c, .cpp …)

​ For non-text files, use byte stream processing (.jpg, .mp3, .mp4, .avi, .doc, .ppt …)

Processing flow

Buffer stream

BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter

Function: Improve the speed of stream reading and writing

When the outer flow is closed, the inner flow will be closed automatically, so the closure of the inner flow can be omitted

Processing flow is "socketing" on the basis of existing flow

public class fileStudy {
    
    
    public static void main(String[] args) {
    
    
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
    
    
            File file = new File("E:\\holiday\\java\\io\\a.txt");
            bis = new BufferedInputStream(new FileInputStream(file));
            bos = new BufferedOutputStream(new FileOutputStream("E:\\holiday\\java\\io\\write.txt"));
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = bis.read(buf)) != -1) {
    
    
                bos.write(buf, 0 ,len);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (bos != null){
    
    
                try {
    
    
                    bos.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
            if (bis != null){
    
    
                try {
    
    
                    bis.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

Conversion flow

InputStreamReader, OutputStreamWriter (belonging to character stream)

Role: Provide conversion between byte stream and character stream

The character set in the conversion stream depends on the encoding format when the file is saved

Standard input and output streams

System.in standard input stream, input from keyboard by default

System.out standard output stream, output from the console by default

The System class uses setIn (InputStream), setOut (OutputStream) to re-specify the input and output methods

Print stream

PrintStream、PrintWriter

data flow

DataInputStream、DataOutputStream

Used to read or write variables or strings of basic quantity types

Object stream

ObjectInputStream、ObjectOutputStream

The processing flow for reading and storing basic data types or objects is powerful in that objects in Java can be written to the data source, and objects can be restored from the data source.

Serialization: A mechanism for storing basic data or objects with the ObjectOutputStream class .

Deserialization: A mechanism for reading basic data or objects with the ObjectInputStream class .

Object serialization mechanism: Allows java objects in memory to be converted into platform-independent binary streams, thereby allowing this binary stream to be permanently stored on disk, or to transmit this binary stream to another network node through the network. When other programs acquire this binary stream, it can be restored to the original java object.

Random Access File Stream

RandomAccessFile implements the two interfaces DataInput and DataOutput, which means that this class is both readable and writable. This object contains a record pointer, which is used to identify the current reading and writing position.

getFilePointer() Get the current position of the file record pointer

If this stream is an output stream, if the written file does not exist, it will be automatically created during operation.

​ If the written file exists, the original content will be overwritten (overwrite from scratch by default).

There is an array inside ByteArrayOutputStream for characters that will not be garbled, it is recommended to use

Guess you like

Origin blog.csdn.net/weixin_45734378/article/details/112861085