生成文件里面的内容是乱七八糟的数据,我想显示正常数据可以吗,如果可以应该改动哪的代码,求教。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class FileOutPutStream{
/**
* 在网络流中轻松地传递对象
*/
public static void main(String[] args) throws Exception {
Student stu1 = new Student("zhangsan", 19, 22);
Student stu2 = new Student("zhengzheng", 20, 23);
FileOutputStream fos = new FileOutputStream("student.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(stu1);
os.writeObject(stu2);
os.close();
FileInputStream fes = new FileInputStream("student.txt");
ObjectInputStream is = new ObjectInputStream(fes);
stu1 = (Student) is.readObject();
stu2 = (Student) is.readObject();
is.close();
//下面只是打印测试
System.out.println("name" + stu1.getName());
System.out.println("age" + stu1.getAge());
System.out.println("score" + stu1.getScore());
System.out.println();
System.out.println("name" + stu2.getName());
System.out.println("age" + stu2.getAge());
System.out.println("score" + stu2.getScore());
}
} |