关于IO流2

1.ObjectOutputStream可以将序列化的对象存储到内存中,ObjectInputStream则是读取文件中序列化保存的对象。
2.PrintWriter跟PrintStream差不多,可以将文件或字符存储到指定路径中。
3.在FileOutputStream中可以在文件路径后面加上true或false选择是否为追加,不填默认为false。
package study02;

import java.io.*;

public class IOPrintStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //ObjectInputStream可以使序列化的对象存储到路径中
        FileOutputStream fos = new FileOutputStream("D:/aa.txt",true);
/*        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fos);
        objectOutputStream.writeObject(new User("123"));*/


        FileInputStream fis = new FileInputStream("D:/aa.txt");
        //ObjectInputStream可以从路径中读取序列化储存的对象
/*        ObjectInputStream stream = new ObjectInputStream(fis);
        User user = (User)stream.readObject();
        String name = user.getName();
        System.out.println(name);*/

        //这两个都差不多,输出流,处理流
        PrintStream ps = new PrintStream(fos);
        ps.print(123);
        ps.flush();
        ps.close();
        PrintWriter pw = new PrintWriter(fos);
        pw.print(1233);
        pw.flush();
        pw.close();
    }
}

 

posted @ 2021-03-09 19:00  温柔Rarry  阅读(33)  评论(0)    收藏  举报