【问题标题】:Java InputStream with different Object Classes具有不同对象类的 Java InputStream
【发布时间】:2018-06-03 13:46:45
【问题描述】:

我的代码必须通过 ObjectOutputStream 读取两种不同的对象类型(Bestellung、AKunde)并将其保存在一个 csv 文件中,这样才有效。 但是当我尝试从文件中读取它们时它不起作用。 代码如下:

输出流:

LinkedList<Bestellung> bestellListe = verwaltungBestell.getBestellListe();
        try {
            fileOutputStream = new FileOutputStream(file);
            outputStream = new ObjectOutputStream(fileOutputStream);

            for (AKunde kunde : kundenliste) {
                outputStream.writeObject(kunde);
            }
            for (Bestellung bestellung : bestellListe) {
                outputStream.writeObject(bestellung);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

输入流:

ArrayList<AKunde> kundenImport = new ArrayList<AKunde>();
ArrayList<Bestellung> bestellungenImport = new ArrayList<Bestellung>();
    boolean cont = true;
    try {

ObjectInputStream objectStream = new ObjectInputStream(new FileInputStream(directorie));         
        while (cont) {
            AKunde kunde = null;
            try {
                kunde = (AKunde) objectStream.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();

            }
            if (kunde != null) {
                kundenImport.add(kunde);
            } else {
                cont = false;
            }
        }
        while (cont) {
            Bestellung bestellung = null;
            try {
                bestellung = (Bestellung) objectStream.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();

            }
            if (bestellung != null) {
                bestellungenImport.add(bestellung);
            } else {
                cont = false;
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block

    }

但它不会读取“Bestellungen”,也不会将它们保存到“bestellungenImport”中。 谁有解决办法???

【问题讨论】:

  • 您需要通过提供确切的问题来澄清
  • 执行代码时会发生什么?
  • 当我执行“AKunden”时,会添加到“kundenImport”,但“Bestellung 对象”不会添加到“bestellungenImport”中
  • @luk2302 在他的回答中解释了原因。

标签: java stream inputstream outputstream fileinputstream


【解决方案1】:

您的代码永远不会到达Bestellung 阅读器部分。

您错误地假设 kunde =(AKunde)objectStream.readObject(); 返回 null。

相反,它会引发异常。

你可以做的就是像 @luk2302 一样投射它。

另一种方法是在编写对象流时添加对象计数:

outputStream.writeInt(kundenliste.size());
for (AKunde kunde : kundenliste) {
    outputStream.writeObject(kunde);
}

outputStream.writeInt(bestellListe.size());
for (Bestellung bestellung : bestellListe) {
    outputStream.writeObject(bestellung);
}

然后用 for each 循环替换您的 while(cont) 循环:

int kundeCount = objectStream.readInt();
for (int i = 0; i < kundeCount; i++) {
   // Read and import kunde
}

【讨论】:

    【解决方案2】:

    您需要更改读取对象的逻辑。主要有两个问题:

    • 您永远不会重置 cont,因此第二个 while 循环将永远不会执行任何操作
    • 即使您这样做了,您也总是会跳过第一个 Bestellung,因为在到达第二个循环时它已经被读取

    我会提出以下建议:

    Object object = objectStream.readObject();
    if (object instanceof AKunde) {
        kundenImport.add((AKunde) object);    
    } else if (object instanceof Bestellung) {
        bestellungenImport.add((Bestellung) object);
    } else {
        // something else was read
    }
    

    您只需要循环这段代码并在需要的地方添加适当的错误处理。

    【讨论】:

    • 所以我实现了你的 if 子句,但“Bestellungen”仍然不会被添加到 bestellungenImport 列表中。
    【解决方案3】:

    我建议,您首先将编写对象的方式更改为ObjectOutputStream

    直接写入kundenListebestellListe对象,这样再次读取对象时就不用担心元素的类型或数量了。然后,您的对象流始终包含两个对象,即两个列表。

    // use try-with-resources if you're on Java 7 or newer
    try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file))) {
        // write the complete list of objects
        outputStream.writeObject(kundenliste);
        outputStream.writeObject(bestellListe);
    } catch (IOException e) {
        e.printStackTrace(); //TODO proper exception handling
    }
    

    那么你可以这样读:

    ArrayList<AKunde> kundenImport = new ArrayList<>();
    ArrayList<Bestellung> bestellungenImport = new ArrayList<>();
    
    //again try-with-resources
    try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file))) {
        kundenImport.addAll((List) inputStream.readObject());
        bestellungenImport.addAll((List) inputStream.readObject());
    } catch (IOException | ClassNotFoundException  e) { //multi-catch, if Java 7 or newer
        e.printStackTrace(); //TODO proper exception handling
    }
    

    进一步阅读:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      相关资源
      最近更新 更多