Learning of serialVersionUID and serialization of fields in ArrayList
Abnormal.
class TestSeriable implements Externalizable {
public transient String name;
public String pwd;
public int age;public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //序列化对象 @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(name); out.writeObject(pwd); out.writeInt(age); } //反序列化对象 @Override public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException { name=(String)in.readObject(); pwd=(String)in.readObject(); age=in.readInt(); } //被序列化的类:必有<a href="https://www.jb51.cc/tag/publiclei/" target="_blank" class="keywords">public类</a>型的无参构造器。因反序列化时,先<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>此<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>实例化<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>类,再<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>readExternal<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>读取<a href="https://www.jb51.cc/tag/shuxing/" target="_blank" class="keywords">属性</a>值。 public TestSeriable(){ }
}
public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable ts=new TestSeriable();
ts.setName("cxh");
ts.setPwd("123456");
ts.setAge(3);
//序列化
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test"));
out.writeObject(ts);//反序列化 ObjectInputStream in = new ObjectInputStream(new FileInputStream("test")); ts = (TestSeriable)in.readObject(); //<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>: Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("name:"+ts.name); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("pwd:"+ts.pwd); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("age:"+ts.age); //<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a>连接 out.close(); in.close(); }
}
<code class="language-java"><span style="font-size:14px;">name:cxh
pwd:123456
age:3Process finished with exit code 0
class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;
private transient String pwd;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 getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; }
}
public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");
test.setPwd("123456");ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("file.txt")); oos.writeObject(test); oos.close(); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt")); TestSeriable ts=(TestSeriable) ois.readObject();//readObject返回final类型的Object对象,需要强制转化 Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my name:"+ts.getName()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my age:"+ts.getAge()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my pwd:"+ts.getPwd()); }
}
Process finished with exit code 0
class TestSeriable implements Serializable {
private String name;
private int age;
private transient String pwd;private String school;//新增字段 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 getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; }
}
public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");
test.setPwd("123456");// ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("file.txt"));
// oos.writeObject(test);
// oos.close();ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt")); TestSeriable ts=(TestSeriable) ois.readObject();//readObject返回final类型的Object对象,需要强制转化 Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my name:"+ts.getName()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my age:"+ts.getAge()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my pwd:"+ts.getPwd()); }
}
Process finished with exit code 1
class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;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 static long getSerialVersionUIDs() { return serialVersionUIDs; }
}
public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("file1.txt")); oos.writeObject(test); oos.close(); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file1.txt")); TestSeriable ts=(TestSeriable) ois.readObject();//readObject返回final类型的Object对象,需要强制转化 Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my name:"+ts.getName()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my age:"+ts.getAge()); }
}
Process finished with exit code 0
class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;
private String address;//新增字段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; } //新增<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a> public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }
}
public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("file.txt")); oos.writeObject(test); oos.close(); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt")); TestSeriable ts=(TestSeriable) ois.readObject();//readObject返回final类型的Object对象,需要强制转化 Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my name:"+ts.getName()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my age:"+ts.getAge()); }
}
Process finished with exit code 0
class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;
private static String address;//更改类型为static字段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; } //新增<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a> public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }
}
public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
TestSeriable test=new TestSeriable();
test.setAge(27);
test.setName("cxh");
test.setAddress("海淀");ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("file.txt")); oos.writeObject(test); oos.close();
// ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt"));
// TestSeriable ts=(TestSeriable) ois.readObject();//readObject返回final类型的Object对象,需要强制转化
// System.out.println("my name:"+ts.getName());
// System.out.println("my age:"+ts.getAge());
// System.out.println("my address:"+ts.getAddress());
}
}
class TestSeriable implements Serializable {
private static final long serialVersionUID=1L;
private String name;
private int age;
private static String address;//更改类型为static字段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; } //新增<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a> public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }
}
public class Main {
public static void main(String[] args) throws Exception{
//Scanner scanner=new Scanner(system.in);//在线笔试
// TestSeriable test=new TestSeriable();
// test.setAge(27);
// test.setName("cxh");
// test.setAddress("海淀");
//
// ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("file.txt"));
// oos.writeObject(test);
// oos.close();ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file.txt")); TestSeriable ts=(TestSeriable) ois.readObject();//readObject返回final类型的Object对象,需要强制转化 Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my name:"+ts.getName()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my age:"+ts.getAge()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("my address:"+ts.getAddress()); }
}
Process finished with exit code 0