package com.model.work;
import java.io.*;
import java.util.Properties;
/**
* @Description:测试类
* @Author: 张紫韩
* @Crete 2021/6/19 15:13
*/
public class WorkHomeDemo03 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\Properties\\dog.properties";
Properties properties = new Properties();
properties.load(new FileReader(filePath));
int age = Integer.parseInt(properties.getProperty("age"));
Dog dog=new Dog(properties.getProperty("name"), age, properties.getProperty("color"));
System.out.println(dog.toString());
System.out.println("姓名:"+dog.getName());
System.out.println("年龄:"+dog.getAge());
System.out.println("颜色:"+dog.getColor());
String dogFile="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\Properties\\dog.dat";
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(dogFile));
objectOutputStream.writeObject(dog);
objectOutputStream.close();
System.out.println("序列化完成");
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(dogFile));
Dog readDog = (Dog) objectInputStream.readObject();
System.out.println(readDog.toString());
objectInputStream.close();
System.out.println("反序列化完成");
}
}
class Dog implements Serializable {
private String name;
private int age;
private String color;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Dog(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
}