public class StreamFile {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//序列化对象list集合
method02();
//反序列化
method03();
}
private static void method03() throws IOException, ClassNotFoundException {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("src\\003.txt"));
Object readObject = objectInputStream.readObject();
List<Persion> readObject1 = (List<Persion>) readObject;
System.out.println(readObject1);
for (Persion persion : readObject1) {
System.out.println(persion);
}
}
private static void method02() throws IOException{
ArrayList<Persion> arrayList = new ArrayList<>();
arrayList.add(new Persion("ancen",26));
arrayList.add(new Persion("huwei",23));
arrayList.add(new Persion("huwei3",23));
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("src\\003.txt"));
outputStream.writeObject(arrayList);
outputStream.close();
}
}
输出指定位置的打印流
public class StreamFile {
public static void main(String[] args) throws IOException, ClassNotFoundException {
PrintStream printStream = new PrintStream("src\\0034.txt");
printStream.println("ancen");
System.setOut(printStream);
printStream.close();
}
}