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