黑马程序员
transien关键字–瞬态关键字
InvalidClassException异常–原理和解决方案
序列化集合
package ObjectStream;
import java.io.*;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class TestArrayStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Person> people = new ArrayList<>();
people.add(new Person("小美",20));
people.add(new Person("小红",20));
people.add(new Person("小金",20));
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("F:\\杂七杂八\\array.txt"));
objectOutputStream.writeObject(people);
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("F:\\杂七杂八\\知网\\array.txt"));
Object o = objectInputStream.readObject();
ArrayList<Person> p=(ArrayList<Person>)o;
for (Person person : p) {
System.out.println(people);
}
}
}
打印流
package ObjectStream;
import java.io.IOException;
import java.io.PrintStream;
public class PrintStreamDemo {
public static void main(String[] args) throws IOException {
PrintStream printStream = new PrintStream("F:\\杂七杂八\\printStream.txt");
printStream.write(97);
printStream.write("I Love You Forever!".getBytes());
printStream.println(98);
printStream.println("I Love You Too!!");
printStream.close();
}
}
aI Love You Forever!98
I Love You Too!!
package ObjectStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class ChangePrintStream {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("我在控制台输出");
PrintStream printStream = new PrintStream("F:\\杂七杂八\\目的地是打印流.txt");
System.setOut(printStream);
System.out.println("我在文件中输出");
printStream.close();
}
}