Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中从文件中反序列化对象_Java_Serialization_Deserialization - Fatal编程技术网

在Java中从文件中反序列化对象

在Java中从文件中反序列化对象,java,serialization,deserialization,Java,Serialization,Deserialization,我有一个包含多个XYZ类序列化对象的文件。序列化时,每个XYZ对象都会附加到文件中 现在我需要从文件中读取每个对象,并且我只能读取第一个对象 知道如何从文件中读取每个对象并最终将其存储到列表中吗?尝试以下方法: List<Object> results = new ArrayList<Object>(); FileInputStream fis = new FileInputStream("cool_file.tmp"); ObjectInputStream ois =

我有一个包含多个XYZ类序列化对象的文件。序列化时,每个XYZ对象都会附加到文件中

现在我需要从文件中读取每个对象,并且我只能读取第一个对象

知道如何从文件中读取每个对象并最终将其存储到列表中吗?

尝试以下方法:

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();
}

不能将ObjectOutputStreams附加到文件。它们包含标题以及您编写的对象。修改你的技巧

此外,您的EOF检测是错误的。您应该单独捕获EOFEException。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;
                      }  
System.out.println(“Nombre del archivo?”);
nArchivo=sc.next();
sc.nextLine();
arreglo=newarraylist();
试一试{
FileInputStream fileIn=新的FileInputStream(nArchivo);
ObjectInputStream in=新的ObjectInputStream(fileIn);
while(true){
在.readObject()中添加((超级英雄);
}
}
捕获(IOI异常)
{
System.out.println(“没有元素\n元素cargados desde el archivo:”);

对于(int w=0;w,您可以按照下面提到的代码以不同方式处理文件结尾:

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();
    }

}

它将从文件中写入和读取多个整数对象,而不会引发任何异常。

您能否提供一个代码片段来说明问题?这些对象是在单个会话中写入的(使用单个
ObjectOutputStream
实例),还是存在多个会话(为每个对象创建、使用和关闭
ObjectOutputStream
)?如果对象是用一个
ObjectOutputStream
写入的,这将起作用。如果使用多个
ObjectOutputStream
连续写入同一个文件,则不会起作用。@TomAnderson如此正确…嗯,修复了。我们实际上还不知道对象是单独写入的。您的第一个答案可能完全正确!我我不知道在过去的4年里是否有什么变化,但当我尝试使用几个ObjectOutputStreams时,它对我起到了作用。每次我写入文件时,我都会确保添加,比如:fos=新文件输出流(FILENAME,true);
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();
    }

}