dog.properties
name=tom age=5 color=red
public class Homework03{
public static void main(string[] args) throws IOException {
String filePath = "src\dog.properties";
Properties properties = new Properties();
properties.load(new FildReader(filePath));
String name = properties.get("name") + "";//Object -> String
int age = Integer.parseInt(properties.get("age") + "");//Object -> int
String color = properties.get("color") + "";//Object -> String
new dog = new Dog(name, age, color);
System.out.println("===dog对象信息===");
System.out.println(dog);
//序列化
String serFilePath = "e:\dog.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFilePath));
oos.writeObject(dog);
//关闭流
oos.close();
System.out.println("dog对象序列化完成..");
}
//(4)反序列化
//在编写一个方法,反序列化dog
public void m1(){
String serFilePath = "e:\dog.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFilePath ));
Dog dog = (Dog) ois.readObject();
System.out.println("===反序列化后 dog===");
System.out.println(dog);
ois.close();
}
}
//Dog类,必须实现Serializable接口
class Dog implements Serializable{
private string name;
private int age;
private string color;
public Dog(String name, int age, String color){
this.name = name;
this.age = age;
this.color = color;
}
@Override
public String toString(){
return "Dog{" +
"name='" + name +''' +
", age=" + age +
", color='" + color + ''' +
'}';
}
}
本文是学习B站韩顺平老师 IO流 视频的做题笔记,可以参考老师讲解视频:java IO流 韩顺平练习题3



