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

发布于 2024-11-18 23:43:00 字数 1272 浏览 1 评论 0原文

我正在尝试使用以下方法读取 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;
}

有点这行不通。它无法转换为我保存的第二个数组列表类型。那么我怎样才能访问这个呢?我得到的确切错误是这样的:

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 isrefered is:

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

So how can I read 2 different arraylists from one file?

I am trying to read 2 arraylists using the following methods.

 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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-11-25 23:43:00

写入第二个文件时使用 FileOutputStream fos = new FileOutputStream("minos.dat", true);true 是参数“append”的值。否则您将覆盖文件内容。这就是您将同一本书读两遍的原因。

当您从文件中读取第二个集合时,您必须跳到第二个集合的开头。为此,您可以记住在第一阶段读取了多少字节,然后使用方法 skip()

但更好的解决方案是仅打开文件一次(我的意思是调用新的 FileInputStream 和新的 FileOutputStream)一次,然后将其传递给读取集合的方法。

Use FileOutputStream fos = new FileOutputStream("minos.dat", true); when writing the second file. true is a value of argument "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().

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.

青春有你 2024-11-25 23:43:00

您可以使用 从文件中读取两个不同的对象ObjectInputStream,但您的问题来自于您重新打开流,因此它从具有 ArrayList 的文件开头开始,并且然后你ArrayList。尝试一次执行所有操作并返回两个列表:

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);
}

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>. 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文