2 qq 33647211 qq_33647211 于 2016.04.19 23:43 提问

java中对象流objectstream问题

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

4个回答

xiaochaolovedan
xiaochaolovedan   2016.04.20 16:06
已采纳

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

qq_33647211
qq_33647211   2016.04.19 23:44

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

xiaochaolovedan
xiaochaolovedan   2016.04.20 09: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
xiaochaolovedan   2016.04.20 09:30

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

xiaochaolovedan
xiaochaolovedan 回复qq_33647211: 你把我的代码粘过去,看看是什么结果
一年多之前 回复
qq_33647211
qq_33647211 我的却不能
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!