java序列化
程序员文章站
2024-02-26 21:32:04
...
java序列化
先上效果图:
该效果是通过俩个类完成:
Emp类:
package com.emp;
import java.io.Serializable;
public class Emp implements Serializable{
/**
* 生成***
*/
private static final long serialVersionUID = -7780805752915363228L;
private String empId;
private String name;
private Double salary;
private String idCard;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
}
ObjectStreamDemo类:
package com.emp;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectStreamDemo {
public static void main(String[] args) {
Emp emp = new Emp();
emp.setEmpId("007");
emp.setName("与其终");
emp.setSalary(new Double(100000));
emp.setIdCard("430281200000000000");
ObjectOutputStream objOutput = null;
ObjectInputStream objInput = null;
try {
//对象序列化
objOutput = new ObjectOutputStream(new FileOutputStream("D:\\emp.data"));
objOutput.writeObject(emp);
objOutput.flush();
objOutput.close();
objInput = new ObjectInputStream(new FileInputStream("D:\\\\emp.data"));
Emp emp2 = (Emp) objInput.readObject();
objInput.close();
System.out.println(emp2.getEmpId());
System.out.println(emp2.getName());
System.out.println(emp2.getSalary());
System.out.println(emp2.getIdCard());
} catch (Exception e) {
// TODO: handle exception
}
}
}
我是与其终,一个风起云涌的女孩子。
很高兴你能看到我的博客,希望能对你有所帮助。