qq_33647211
qq_33647211
采纳率50%
2016-04-19 15:43 阅读 1.5k
已采纳

java中对象流objectstream问题

输出的为什么不是cyh男20
ym女20
求大神解答!!!!!!!!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    xiaochaolovedan 程序猿的人生 2016-04-20 08:06

    你的代码和我这个一样吗?麻烦把你的代码粘全了,我看看

    点赞 评论 复制链接分享
  • qq_33647211 qq_33647211 2016-04-19 15:44

    这个是照片。。。。。。。。。图片图片

    点赞 评论 复制链接分享
  • xiaochaolovedan 程序猿的人生 2016-04-20 01:29

    我和你写的差不多,不知道你为啥会这样,我给你粘出我的代码
    package lianxi;

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;

    public class TestObjectOutPutString{
    public static void main(String[] args) throws IOException{
    Person p = new Person("wang", "boy", 20);
    Person p1 = new Person("ma", "girl", 21);
    try {
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d:/1.txt"));
    out.writeObject(p);
    out.writeObject(p1);
    out.flush();
    out.close();

            ObjectInputStream in = new ObjectInputStream(new FileInputStream("d:/1.txt"));
            while(true){    
                System.out.println(in.readObject());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    

    }
    class Person implements Serializable {
    private String name;
    private String gender;
    private int age;
    public Person(String name, String gender, int age) {
    super();
    this.name = name;
    this.gender = gender;
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getGender() {
    return gender;
    }
    public void setGender(String gender) {
    this.gender = gender;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    @Override
    public String toString() {
    return "Person [name=" + name + ", gender=" + gender + ", age=" + age
    + "]";
    }

    }

    点赞 评论 复制链接分享
  • xiaochaolovedan 程序猿的人生 2016-04-20 01:30

    结果就是
    Person [name=wang, gender=boy, age=20]
    Person [name=ma, gender=girl, age=21]

    点赞 评论 复制链接分享

相关推荐