1.5.2 修改默认的序列化机制
Serializable 修饰类,阻止部分变量序列化的方式:
使用 static 修饰
使用 transient 关键字修饰
可序列号类读写对象签名方法:
从 Serializable拷贝即可:
一般就用到前两个方法读写,第三个是对于读空数据处理
Main.java
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
import com.dyy.jdk8.test.Employee;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Main solution = new Main();
Employee[] staff = new Employee[3];
staff[0] = new Employee("Carl Cracker", 75000,1987, 12, 15);
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(new File("E:/aa.txt")));
for(Employee clerk:staff){
oo.writeObject(clerk);
}
//反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("E:/aa.txt")));
Employee[] readStaff = new Employee[3];
for(int i = 0; i < 3; i++){
readStaff[i] = (Employee) ois.readObject();
System.out.println(readStaff[i]);
}
}
}
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
Employee.java
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Employee implements Serializable{
private String name;
private double salary;
private Date hireDay;
public static final int NAME_SIZE = 40;
public static final int RECORD_SIZE = 2*NAME_SIZE+20;
public Employee(){}
public Employee(String n, double s, int year, int month, int day){
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year,month-1, day);
hireDay = calendar.getTime();
}
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 Date getHireDay() {
return hireDay;
}
public void setHireDay(Date hireDay) {
this.hireDay = hireDay;
}
public void raiseSalary(double byPercent){
double raise = salary*byPercent/100;
salary+=raise;
}
@Override
public String toString() {
return getClass().getName() + "[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
}
public void writeData(DataOutput out) throws IOException {
DataIO.writeFixedString(name,NAME_SIZE, out);
out.writeDouble(salary);
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(hireDay);
out.writeInt(calendar.get(Calendar.YEAR));
out.writeInt(calendar.get(Calendar.MONTH)+1);
out.writeInt(calendar.get(Calendar.DAY_OF_MONTH));
}
public void readData(DataInput in) throws IOException {
name = DataIO.readFixedString(NAME_SIZE, in);
salary = in.readDouble();
int y = in.readInt();
int m = in.readInt();
int d = in.readInt();
GregorianCalendar calendar = new GregorianCalendar(y,m-1,d);
hireDay = calendar.getTime();
}
private void writeObject(java.io.ObjectOutputStream out)
throws IOException{
out.writeUTF(name);
out.writeDouble(salary);
out.writeLong(hireDay.getTime());
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException{
name = in.readUTF();
salary = in.readDouble();
hireDay = new Date(in.readLong());
}
}
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
运行结果:
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
相关内容:选择 《Java核心技术 卷1》查找相关笔记
评论🌹点赞👍收藏✨关注👀,是送给作者最好的礼物,愿我们共同学习,一起进步