//测试类
public class ObjectStreamDemo {
public static void main(String[] args) throws Exception {
// writrObj();
readObj();
}
/**
* @Title: writrObj
* @Description: 把学生对象 写到电脑文件
* @return: void
* @throws FileNotFoundException
*/
public static void writrObj() throws Exception {
//对象输出流:
ObjectOutputStream objout = new ObjectOutputStream(new FileOutputStream("D:\\文件\\喵.txt"));
objout.writeObject(new Student("Ann", 18));
objout.close();
}
/**
*
* @Title: readObj
* @Description: 读喵文件中的对象
* @return: void
*/
public static void readObj() throws Exception {
ObjectInputStream objin = new ObjectInputStream(new FileInputStream("D:\\文件\\喵.txt"));
Student s=(Student)objin.readObject();
System.out.println(s.toString());
objin.close();
}
}
//学生类
class Student implements Serializable{
private String nameString;
private int age;
@Override
public String toString() {
return "Student [nameString=" + nameString + ", age=" + age + "]";
}
public Student(String nameString, int age) {
super();
this.nameString = nameString;
this.age = age;
}
}
IO流---对象流 对象写出到电脑文件,后 读入控制台
猜你喜欢
转载自blog.csdn.net/weixin_45881192/article/details/104088825
今日推荐
周排行