public static void main(String[] args) throws Exception {
//-----------------第一次写入数据----------------------
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test2", true));
for (int i = 0; i < 5; i++) {
out.writeObject(new String(""+i));
}
out.flush();
out.close();
//-----------------第一次写入数据---------------------- out = new ObjectOutputStream(new FileOutputStream("test2", true));
out.writeObject(new String(""+10)); //这个数据没有输出
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("test2"));
while(true){
try{
System.out.println(in.readObject());
}catch(Exception e){
break;
}
}
}输出结果:0 1 2 3 4
问题:为什么只输出第一次写入的数据,第二次写入的数据没有输出,是不是写入对象流的文件不能被继续写入?
import java.io.*;
public class Test{
public static void main(String[] args) throws Exception {
//-----------------第一次写入数据----------------------
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test2"));
for (int i = 0; i < 5; i++) {
out.writeObject(new String(""+i));
}
out.flush();
out.close();
//-----------------第一次写入数据----------------------
out = new ObjectOutputStream(new FileOutputStream("test3"));
out.writeObject(new String(""+0)); //这个数据没有输出
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("test3"));
}
}
不清楚这个类背后的原理,但是有其它类可以实现LZ的原始目的