IO style

 The purpose of the stream operation: to complete the conversion of memory data and disk data.

Streams are divided into directions: input stream and output stream, with memory as a reference.
When data is read from a data source into memory, it is called an input stream (read stream).
When data in memory is written to a data source, it is called an output stream (write stream).

Streams are divided according to the content of transmission: byte stream, character stream, and object stream.
No matter what kind of stream it is, the underlying part is transmitted in bytes, so its essence is a byte stream. However, in order to facilitate programmers to better operate character data and object data, a layer of packaging is made on the basis of byte stream to form character stream and object stream.


Steps of stream operation:
1.  Create stream;
2.  Operate stream;
3.  Close stream;

after stream operation, the operation must be closed, otherwise not only memory resources are wasted, but also data may not be written to the stream.

When operating a file stream, if the file does not exist, then reading the stream will throw a file not found exception, while writing to the stream will create a new file.


[Parent class of byte stream: InputStream (read stream) ][ OutputStream (write stream)]
// read
public void inputFile() {
InputStream input = null; try {     // create a file to read byte stream     input = new FileInputStream("D:/a.jpg");     byte[] by = new byte[1024];
        



            

    int len ​​= 0;
    //Read the data in the stream into a byte array, return the number of bytes currently read, after reading, return -1
    //read() will be at most by.length from this input stream Read bytes of data into a byte array
    while ((len = input.read(by)) != -1) {
        System.out.println(len);
    }
} catch (Exception e) {
    e.printStackTrace( );
} finally {
    try {
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
//write
public void outputFile(String str) {     OutputStream out = null;     try {         // If the second parameter is true, it means to write data in append mode         out = new FileOutputStream("D:/b.txt",true);         out.write(str.getBytes());
        




            

            
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
//拷贝
public void copyFile() {     InputStream input = null;    OutputStream out = null;    try {        input = new FileInputStream("D:/a.txt");        out = new FileOutputStream("D:/b.txt");        byte[] by = new byte[1024];        int len = 0;        while ((len = input.read(by)) != -1) {            //读取了几个字节,就写入几个字节            out.write(by, 0, len);        }
        





            






    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
            input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
} [character stream Parent class: Reader ][ Writer] //Read public void reader() {     Reader read = null;     BufferedReader bu = null;     try {         //Create a file read stream         read = new FileReader("D:/a.txt" );         //Socket stream, on the basis of a stream, socket another stream, also known as advanced stream         bu = new BufferedReader(read);         String str = null;         while ((str = bu.readLine()) != null) {
    






        







            System.out.println(str);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            bu.close();
            read.close();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
}


//Write
public void writer() {
    Writer wri = null;     try {         //If the second parameter is true, it means appending data         wri = new FileWriter("D:/1.txt ",true);         wri.write("bilibili");     } catch (Exception e) {         e.printStackTrace();     } finally {         try {             wri.close();
        




            





        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Object Stream

Serialization : When a heap object is required for transmission, since the data in the object is too large to be transmitted directly, the object needs to be broken up into The binary sequence is transmitted over and over again. This process is called the serialization process. After reaching the destination, it is necessary to restore the binary sequence to an object. This process is called the deserialization process.


All objects that need to implement object serialization must implement the Serializable interface

//transient is an attribute modifier. After adding transient, it means that the value of the attribute will not be transmitted
private transient int score;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324654103&siteId=291194637