//我将对象student序列化到hello.txt文件为什么 反序列化后student的name和age都成0了啊??import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;public class ObjectOutputStreamTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:\\test\\hello.txt"));
Student s=new Student("安安",20);
oos.writeObject(s);
oos.close();
}
}
//反序列化
public static void main(String[] args) {
try {
//实例化对象输入流
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:\\test\\hello.txt"));
try {
//反序列化对象
Student stu=(Student) ois.readObject();
System.out.println("学生姓名"+stu.getName());
System.out.println("学生年龄"+stu.getAge());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;public class ObjectOutputStreamTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:\\test\\hello.txt"));
Student s=new Student("安安",20);
oos.writeObject(s);
oos.close();
}
}
//反序列化
public static void main(String[] args) {
try {
//实例化对象输入流
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:\\test\\hello.txt"));
try {
//反序列化对象
Student stu=(Student) ois.readObject();
System.out.println("学生姓名"+stu.getName());
System.out.println("学生年龄"+stu.getAge());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
解决方案 »
- 请教:关于SWT/Jace 打包成Jar的问题
- 各位高手进~~~小弟我想做个站内搜索!哪位高手给指点下思路~??
- 十万火急!!!问一个简单问题!!!
- jxl导出Excel,需要多个sheet应该怎么声明
- 如何设计一个架构,使得框架既符合SDO规范,又能用到hibernate庞大的持久化功能?
- 请大家推荐JAVA教材!
- 学java能向哪些方面发展呀?
- 请教如何把jbuilder7.0与sql server2000连接起来。
- JCombox的问题?
- Java Applet小程序怎样向html传参数?
- java 复数类。定义的四个 方法 声明无效需要返回类型。请各位大侠,前辈帮忙看看
- 一道小小的算法题、、高手来
private int age;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public class ObjectOutputStreamTest { public static void main(String[] args) throws FileNotFoundException,
IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"hello.txt")); Student s = new Student("安安", 20); oos.writeObject(s);
oos.flush();
oos.close(); // 反序列化 try {
// 实例化对象输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"hello.txt")); try {
// 反序列化对象
Student stu = (Student) ois.readObject(); System.out.println("学生姓名:" + stu.getName());
System.out.println("学生年龄:" + stu.getAge()); } catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); } }}
class Student implements Serializable{
private String name;
private int age; public Student(String name,int age) {
this.name = name;
this.age = age;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
代码没问题啊。
不过我运行我自己写的,怎么一点问题都没有??你自己检查哪里错了
我只是帮你加了点东西
1.student的构造方法,我相信你也写了,不然会报错。
2.oos.flush,清缓存。