import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
class Student implements Serializable {
private String name;
public Student(String name) {
this.name = name;
}
}
public class test2 {
public static void main(String[] args) throws Exception {
Student s = new Student("WJY");
Student s2 = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
//创建对象输出流
oos = new ObjectOutputStream(new FileOutputStream("b.bin"));
//创建对象输入流
ois = new ObjectInputStream(new FileInputStream("b.bin"));
//序列化java对象
oos.writeObject(s);
oos.flush();
//反序列化java对象
s2 = (Student) ois.readObject();
} finally { //使用finally块回收资源
if (oos != null) {
try {
oos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (ois != null) {
try {
ois.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
public class test2 {
public static void main(String[] args) throws Exception {
Student s = new Student("WJY");
Student s2 = null;
try (//创建对象输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.bin"));
//创建对象输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.bin"));
)
{
//序列化java对象
oos.writeObject(s);
oos.flush();
//反序列化java对象
s2 = (Student) ois.readObject();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有