问题描述
- java中对象流objectstream问题
- 输出的为什么不是cyh男20
ym女20
求大神解答!!!!!!!!!!
解决方案
你的代码和我这个一样吗?麻烦把你的代码粘全了,我看看
解决方案三:
我和你写的差不多,不知道你为啥会这样,我给你粘出我的代码
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
+ ""]"";
}
}
解决方案四:
结果就是
Person [name=wang gender=boy age=20]
Person [name=ma gender=girl age=21]