我有一个文件,其中包含XYZ类的多个序列化对象。在序列化时,每个XYZ对象都附加到文件中。从Java中的文件反序列化对象
现在我需要从文件中读取每个对象,并且只能读取第一个对象。
任何想法如何从文件中读取每个对象并最终将其存储到List中?
我有一个文件,其中包含XYZ类的多个序列化对象。在序列化时,每个XYZ对象都附加到文件中。从Java中的文件反序列化对象
现在我需要从文件中读取每个对象,并且只能读取第一个对象。
任何想法如何从文件中读取每个对象并最终将其存储到List中?
尝试以下操作:
List<Object> results = new ArrayList<Object>();
FileInputStream fis = new FileInputStream("cool_file.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
try {
while (true) {
results.add(ois.readObject());
}
} catch (OptionalDataException e) {
if (!e.eof) throw e;
} finally {
ois.close();
}
汤姆的精彩评论跟进,多解ObjectOutputStream
s就,
public static final String FILENAME = "cool_file.tmp";
public static void main(String[] args) throws IOException, ClassNotFoundException {
String test = "This will work if the objects were written with a single ObjectOutputStream. " +
"If several ObjectOutputStreams were used to write to the same file in succession, " +
"it will not. – Tom Anderson 4 mins ago";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(FILENAME);
for (String s : test.split("\\s+")) {
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);
}
} finally {
if (fos != null)
fos.close();
}
List<Object> results = new ArrayList<Object>();
FileInputStream fis = null;
try {
fis = new FileInputStream(FILENAME);
while (true) {
ObjectInputStream ois = new ObjectInputStream(fis);
results.add(ois.readObject());
}
} catch (EOFException ignored) {
// as expected
} finally {
if (fis != null)
fis.close();
}
System.out.println("results = " + results);
}
调用results.add(ois.readObject()); 在一个循环中 –
如果这些对象是用一个'ObjectOutputStream'编写的,那么这将起作用。如果多个'ObjectOutputStream'被用来连续写入同一个文件,它就不会。 –
@ RockyTriton well spotted :)是的,已经修复了,谢谢! – alf
您不能追加ObjectOutputStreams到一个文件中。它们包含标题以及您编写的对象。修改你的技巧。
此外你的EOF检测是错误的。你应该单独捕获EOFException。 OptionalDataException意味着完全不同的东西。
这对我的作品
System.out.println("Nombre del archivo ?");
nArchivo= sc.next();
sc.nextLine();
arreglo=new ArrayList<SuperHeroe>();
try{
FileInputStream fileIn = new FileInputStream(nArchivo);
ObjectInputStream in = new ObjectInputStream(fileIn);
while(true){
arreglo.add((SuperHeroe) in.readObject());
}
}
catch(IOException i)
{
System.out.println("no hay mas elementos\n elementos cargados desde el archivo:");
for(int w=0;w<arreglo.size();w++){
System.out.println(arreglo.get(w).toString());
}
}catch(ClassNotFoundException c)
{
System.out.println("No encontre la clase Estudiante !");
c.printStackTrace();
return;
}
您可以按照以下提到的代码用于处理文件的结尾不同:
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
// TODO Auto-generated method stub
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E://sample.txt"));
oos.writeObject(new Integer(5));
oos.writeObject(new Integer(6));
oos.writeObject(new Integer(7));
oos.writeObject(new Integer(8));
oos.flush();
oos.close();
ObjectInputStream ios = new ObjectInputStream(new FileInputStream("E://sample.txt"));
Integer temp;
try {
while ((temp = (Integer) ios.readObject()) != null) {
System.out.println(temp);
}
} catch (EOFException e) {
} finally {
ios.close();
}
}
它会写,没有抛出任何从文件中读取多个整数对象例外。
请问您能提供一个能说明问题的代码片段吗? – alf
二进制或XML序列化? – spork
是否所有对象都写在单个会话中(只有一个ObjectOutputStream实例),还是有多个会话(为每个会话创建,使用和关闭一个ObjectOutputStream)? –