首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Java中反序列化文件中的对象

在Java中反序列化文件中的对象
EN

Stack Overflow用户
提问于 2011-10-20 03:16:19
回答 4查看 22.6K关注 0票数 5

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

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

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-10-20 03:26:19

尝试以下操作:

代码语言:javascript
复制
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的解决方案将是:

代码语言:javascript
复制
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);
}
票数 12
EN

Stack Overflow用户

发布于 2011-10-20 05:04:38

您不能将ObjectOutputStreams附加到文件。它们包含标头和您编写的对象。修改你的技术。

你的EOF检测也是错误的。您应该单独捕获EOFException。OptionalDataException的含义完全不同。

票数 1
EN

Stack Overflow用户

发布于 2016-06-29 09:14:50

这对我很管用

代码语言:javascript
复制
  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;
                      }  
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7826834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档