package test01;
import java.io.*;
public class serializableDemo
{
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建对象
Product product = new Product();
//给属性赋值
product.setPname("香香");
product.setPrice(1000);
product.setCategory_id("c008");
//文件对象
File file = new File("text.srs");
//文件输入流和输出流
FileOutputStream fileOutputStream = new FileOutputStream(file);
//对象流
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
//对象写入序列化
objectOutputStream.writeObject(product);
//测试读取序列流
FileInputStream fileInputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//向上专行
Product product2 = (Product)objectInputStream.readObject();
System.out.println(product2.getPname());
}
}
11-05
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交