简体   繁体   English

从文件中读取两个不同的对象

[英]Reading two different objects from file

I am trying to read 2 arraylists using the following methods.我正在尝试使用以下方法读取 2 个数组列表。

 public static ArrayList<Contestant> readContestantsFromFile() throws IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream("minos.dat");
    ObjectInputStream ois = new ObjectInputStream(fis);

    ArrayList<Contestant> contestants = (ArrayList<Contestant>) ois.readObject();

    ois.close();

    return contestants;
}
public static ArrayList<Times> readContestantsFromFile() throws IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream("minos.dat");
    ObjectInputStream ois = new ObjectInputStream(fis);

    ArrayList<times> times = (ArrayList<Times>) ois.readObject();
    ois.close();
    return times;
}

Bit this doesn't work.位这行不通。 It cannot cast to the the second arraylist type i've saved.它不能转换为我保存的第二个 arraylist 类型。 So how can I access this?那么我怎样才能访问它呢? The exact error I got was this:我得到的确切错误是:

Exception in thread "main" java.lang.ClassCastException: com.deanchester.minos.model.Contestant cannot be cast to com.deanchester.minos.model.Times
at com.deanchester.minos.tests.testAddTime.main(testAddTime.java:31)

The line that this is referring to is:这指的是:

ArrayList<times> times = (ArrayList<Times>) ois.readObject();

So how can I read 2 different arraylists from one file?那么如何从一个文件中读取 2 个不同的数组列表呢?

Use FileOutputStream fos = new FileOutputStream("minos.dat", true);使用FileOutputStream fos = new FileOutputStream("minos.dat", true); when writing the second file.写第二个文件时。 true is a value of argument "append". true是参数“append”的值。 Otherwise you override the file content.否则,您将覆盖文件内容。 This is the reason that you read the same collection twice.这就是您阅读同一个集合两次的原因。

When you are reading the second collection from the file you have to skip to the beginning of the second collection.当您从文件中读取第二个集合时,您必须跳到第二个集合的开头。 To do this you can remember how many bytes have you read on first phase and then use method skip() .为此,您可以记住在第一阶段读取了多少字节,然后使用方法skip()

But better solution is to open file only once (I mean call new FileInputStream and new FileOutputStream) only once and then pass it to methods that read collections.但更好的解决方案是只打开一次文件(我的意思是调用新的 FileInputStream 和新的 FileOutputStream)一次,然后将其传递给读取 collections 的方法。

You can read two different objects from a file using a ObjectInputStream , but your problem comes from the fact that you reopen the stream so it starts at the beginning of the file where you have the ArrayList<Contestant> and then you ArrayList<Times> .您可以使用ObjectInputStream从文件中读取两个不同的对象,但您的问题来自您重新打开 stream 的事实,因此它从您拥有ArrayList<Contestant>的文件的开头开始,然后是ArrayList<Times> Try doing everything at once and returning both lists:尝试一次做所有事情并返回两个列表:

public static ContestantsAndTimes readObjectsFromFile() throws IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream("minos.dat");
    ObjectInputStream ois = new ObjectInputStream(fis);

    ArrayList<Contestant> contestants = (ArrayList<Contestant>) ois.readObject();
    ArrayList<Times> times = (ArrayList<Times>) ois.readObject();
    ois.close();

    return new ContestantsAndTimes(contestants, times);
}

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

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