--- IO stream object flow object is written to a computer file, reads the console


//测试类
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;
	}
}



Published 14 original articles · won praise 8 · views 4560

Guess you like

Origin blog.csdn.net/weixin_45881192/article/details/104088825