java序列化与反序列化流的操作,打印流的输出
Posted weixin_ancenhw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java序列化与反序列化流的操作,打印流的输出相关的知识,希望对你有一定的参考价值。
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();
以上是关于java序列化与反序列化流的操作,打印流的输出的主要内容,如果未能解决你的问题,请参考以下文章
Java IO详解(六)------序列化与反序列化(对象流)