在使用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接口.