加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

ArrayList中字段serialVersionUID和序列化的学习

发布时间:2020-12-14 06:39:02 所属栏目:Java 来源:网络整理
导读:异常。 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() { ret

异常。

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();
}
//被序列化的类:必有public类型的无参构造器。因反序列化时,先调用此方法实例化一个类,再调用readExternal方法读取属性值。
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();

    //输出:
    System.out.println("name:"+ts.name);
    System.out.println("pwd:"+ts.pwd);
    System.out.println("age:"+ts.age);

    //关闭连接
    out.close();
    in.close();
}

}


<span style="font-size:14px;">输出结果:
<code class="language-java"><span style="font-size:14px;">name:cxh
pwd:123456
age:3

Process finished with exit code 0


<span style="font-size:14px;">

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对象,需要强制转化
    System.out.println("my name:"+ts.getName());
    System.out.println("my age:"+ts.getAge());
    System.out.println("my pwd:"+ts.getPwd());


}

}


<span style="font-size:14px;">
输出结果:

Process finished with exit code 0

<span style="font-size:14px;">

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对象,需要强制转化
    System.out.println("my name:"+ts.getName());
    System.out.println("my age:"+ts.getAge());
    System.out.println("my pwd:"+ts.getPwd());


}

}


<span style="font-size:14px;">报错信息:

Process finished with exit code 1

<span style="font-size:14px;">

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对象,需要强制转化
    System.out.println("my name:"+ts.getName());
    System.out.println("my age:"+ts.getAge());
}

}

Process finished with exit code 0

<span style="font-size:14px;">

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;
}
//新增方法
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对象,需要强制转化
    System.out.println("my name:"+ts.getName());
    System.out.println("my age:"+ts.getAge());
}

}


<span style="font-size:14px;">
输出结果:

Process finished with exit code 0

<span style="font-size:14px;">

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;
}
//新增方法
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;
}
//新增方法
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());
}

}


<span style="font-size:14px;">
输出结果:

Process finished with exit code 0

<span style="font-size:14px;">

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读