Conversion of object stream and byte stream of IO stream in Java

One, object flow

1. Object stream-with the help of byte stream

 @Test//对象流--借助于字节流
    public void test2() throws Exception {
    
    
        //1.将java对象写入文本中保存--序列化
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/a.jpg"));
        oos.writeObject(new Monkey("狒狒"));

        //2.将文本中保存的对象读取到程序中--反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/a.jpg"));
        Monkey m=(Monkey)ois.readObject();
        System.out.println(m);
    }

Insert picture description here

Second, the conversion of byte stream

1. Convert byte stream to character stream and buffer stream

The code is as follows (example):

@Test
    public void test1() throws Exception {
    
    
        //字节流-->字符流的转换
        Reader ir = new InputStreamReader(new FileInputStream("src/a.txt"));
        //转换字符缓冲流
        BufferedReader br = new BufferedReader(ir);
        br.readLine();
    }

to sum up

The above is the related content of object stream and character stream conversion, which mainly realizes the combination between stream and object.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/112001921