【问题标题】:Read a serialized object as a stream将序列化对象作为流读取
【发布时间】:2016-07-01 09:06:58
【问题描述】:

我以这种方式序列化了一个巨大的链表(150mb):

public void serialize(ArrayList<LinkedList> e, String file){    
try {
        FileOutputStream fileOut =
                new FileOutputStream("file+".ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(e);
        out.close();
        fileOut.close();

    }catch(IOException i)
    {
        i.printStackTrace();
    }
}

现在,我想以非阻塞方式将其作为流读取,这与以下方法相反:

public ArrayList deserialize(File file){
    ArrayList<LinkedList> e = null;
    try
    {
        FileInputStream fileIn = new FileInputStream(file);
        ObjectInputStream in = new ObjectInputStream(fileIn);
        e = (ArrayList<LinkedList>) in.readObject();
        in.close();
        fileIn.close();
        return e;
    }catch(IOException i)
    {
        i.printStackTrace();
        return null;
    }catch(ClassNotFoundException c)
    {
        System.out.println("Object not found");
        c.printStackTrace();
        return null;
    }
}

是否可以在反序列化过程仍在运行且尚未完成时使用部分反序列化的数据?

【问题讨论】:

  • 没有。这是不可能的。但是您可以改为循环遍历您的列表并分别编写每个元素,然后分别读回每个元素。或者选择比 Java 序列化更好的格式。用于长期存储的 Java 序列化是个坏主意。

标签: java serialization


【解决方案1】:

你不能。没有像文件的非阻塞 I/O 这样的东西。你的问题没有意义。

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多