简体   繁体   English

从文件读取对象到输入流

[英]Reading Objects From A File to An Input Stream

I am presently working on a simple ObjectInputStream and ObjectOutputStream , I have read both the documentation and the Java tutorial and am familiar with the basics; 我目前正在研究一个简单的ObjectInputStreamObjectOutputStream ,已经阅读了文档和Java 教程,并熟悉了基础知识。 however, in attempting to compile my program, I am encountering an error that may be related to my misunderstanding of the combination of Map s and Object input/output , specifically the input portion. 但是,在尝试编译程序时,遇到了一个错误,该错误可能与我Map和对象输入/输出 (特别是输入部分)的组合的误解有关。

I have a .dat file, from which I am trying to read a list of objects that get mapped into a TreeMap : 我有一个.dat文件,试图从中读取映射到TreeMap的对象列表:

public class Product implements Serializable 
{
    private static final long serialVersionUID = 1L;
    private int code;
    private String name;
    private int quatity;

    // Setters and Getters

}

Above, is the code fragment for the Product object, itself - implementing Serializable . 上面是Product对象本身的代码片段-实现Serializable I include the fragment in case the problem lies, there. 如果出现问题,我会在其中添加片段。

For this question, assume the .dat is not empty and contains properly formatted data. 对于此问题,假定.dat不为空,并且包含格式正确的数据。

Here is my ObjectInputStream code: 这是我的ObjectInputStream代码:

try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file))) {
    while (true) {
        try {
            products = (Map<Integer, Product>) inputStream.readObject();
        }
        catch (ClassNotFoundException cnfException  {      
            System.out.println("ClassNotFoundException: " + cnfException.getMessage());
        }
        catch (EOFException eofException) {
            System.err.println("EOFException: " + eofException.getMessage());
        }   
}

When attempting to run this code, I get the following error (a Cast error): 尝试运行此代码时,出现以下错误(强制转换错误):

错误消息的屏幕截图

Here is how I am writing Product objects to the .dat file: 这是我将Product对象写入.dat文件的方式:

try (ObjectOutputStream outputStream = new ObjectOutputStream(new    FileOutputStream(fileName))) {
    for (int i = 0; i < products.size(); i++) {
        outputStream.writeObject(products.get(i));
    }
}

Having isolated the error, I know the error occurs when I hit the products = portion. 隔离出错误后,我知道当我点击products =部分时就会发生错误。 I am unsure if this is a compound issue or if this is one of two issues: 我不确定这是复合问题还是这是两个问题之一:

  1. I am not properly grabbing the data from the file in order to populate the TreeMap 我没有正确地从文件中获取数据以填充TreeMap
  2. I am misunderstanding the implementation of ObjectInputStream 我误解了ObjectInputStream的实现

It sounds like you originally just wrote the Product objects to an ObjectOutputStream , rather than a Map<Integer, Product> . 听起来您最初只是将Product对象写入ObjectOutputStream ,而不是Map<Integer, Product> If that's the case, you need something like: 如果是这样,您需要类似以下内容:

Map<Integer, Product> products = new TreeMap<>();
try (ObjectInputStream input = new ObjectInputStream(new FileInputStream(file))) {
    while (true) {
        Product product = (Product) input.readObject();
        products.put(product.getCode(), product); // Or whatever
    }
} catch (EOFException e) {
    // Just finish? Kinda nasty...
}

Of course, that will throw an exception when it reaches the end of the stream - you might want to think about how you're going to detect that cleanly rather than just handling the exception. 当然,这将在流到达末尾时引发异常-您可能需要考虑如何干净地检测到该异常,而不是仅仅处理异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM