ObjectStream的使用

在使用object流時,必須要實現了Serializable接口才可以.


<span style="font-size:18px;">public class Text implements Serializable {
	
}
</span>


<span style="font-size:18px;">public class Test implements Serializable {
	public static void main(String[] args) {
		FileOutputStream fos;
		ObjectOutputStream oos;
		try {
			fos=new FileOutputStream("log.txt");
			oos=new ObjectOutputStream(fos);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		oos.writeObject(new Student());
		oos.flush();
		oos.close();
	}
}
</span>

如果是存入的是容器等,必須要保證容器內的每一對象都實現了Serializable接口.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章