JAVA learning - special operation stream: standard input and output stream, print stream, object serialization and deserialization stream

special operation flow

standard input and output stream

The System class has two static member variables:
public static final InputStream in: standard input stream, usually corresponding to keyboard input or another input source specified by the host environment

//字节输入流
        InputStream is = System.in;
/*        实现字节的持续输入
        int by;
        while((by= is.read())!= -1){
            System.out.println((char)by);
        }*/
//        用转换流把字节流转成字符流
        InputStreamReader isr = new InputStreamReader(is);
//使用字符流实现一次读一行数据(字符缓冲输入流读特有方法)
        BufferedReader br = new BufferedReader(isr);
//实现按行的字符的输入
            String str = br.readLine();
            System.out.println("看这里" + str);
//可以用Scanner实现各种类型数据的输入
        Scanner sc = new Scanner(System.in);

public static final PrintStream out: Standard output stream
Adds functionality to another output stream, that is, it can conveniently print representations of various data values;
the essence of the output statement is a standard output stream
PrintStream ps = System.out;
PrintStream class The method, System.out can be used

print stream

has its own special method

PrintStream byte print stream

PrintStream(String fileName): Use the specified file name to create a new print stream.
Use the method inherited from the parent class to write data, and it will be transcoded when viewing;
use your own unique method to write data, and the viewed data will be output as it is.

PrintWriter character print stream

Character print stream construction method
code example

//自动刷新输入的数据
        PrintWriter pw = new PrintWriter(new FileWriter("1215/file.txt"),true);
        pw.write("haha");
        pw.println("zenmebamn");

Copy file code example:

        BufferedReader br = new BufferedReader(new FileReader("1215/file.txt"));
        //创建字符打印流
        PrintWriter pw = new PrintWriter(new FileWriter("1215/file2.txt"),true);
        String line;
        while((line = br.readLine())!= null){
    
    
            pw.println(line);
        }
        br.close();
        pw.close();

Object serialization stream and deserialization stream

Object serialization: save the object to disk, or transmit it in the network
Object serialization stream: ObjectOutputStream, the class to which the object belongs needs to implement the Serializable interface, which is a marker interface and does not need to rewrite the method;

Object deserialization: read back from the file, reconstruct the object;
object deserialization stream: ObjectInputStream
can deserialize the object written by ObjectOutputstream

The collection class of the Properties-Map system can be stored in the stream, that is, it can be linked with the content in the file

Can be saved to or loaded from a stream. Each key in the property list and its corresponding value is a string; not a generic class;
insert image description here
insert image description here
void store (OutputStream out/Writer writer, String comments) writes the contents of the properties table into the file corresponding to the byte stream In the middle, the format suitable for using the void load method is written to the properties table with the output stream;

Guess you like

Origin blog.csdn.net/weixin_52723971/article/details/111265575