【问题标题】:EOFException when I extract the Object Input/Out put Stream提取对象输入/输出流时出现 EOFException
【发布时间】:2019-10-03 10:21:30
【问题描述】:

我使用 ObjectInput/Output 来初始化名为 temp 的 hashmap,并将名为 map 的 hashmap 的所有条目初始化为 new,然后使用 OutputStream 将其保存为文件格式为 .ser 这工作完美......

import java.io.*;
import java.util.HashMap;

public class PlayerInfo implements Serializable {

    ObjectOutputStream out;
    ObjectInputStream in;
    File userData =new File("path.ser");
    HashMap map ;
    HashMap temp;
    private Integer ID;
    String name ;
    boolean isItNull =false;

    public static void main(String[] args) {
        new PlayerInfo();
    }
    PlayerInfo(){
        try {
        initializeHashMap();

        }catch (Exception e){
            e.printStackTrace();
        }

    }
    @SuppressWarnings("unchecked")
    private void initializeHashMap(){

    try {
//initialize ObjectInputStream in same method when I use it and close it then
        in =new ObjectInputStream(new FileInputStream(userData));

        if (isItNull){
            temp =new HashMap<Integer,PlayerInfo>();

        }else {
            map =new HashMap<Integer,PlayerInfo>();
            temp = (HashMap<Integer, PlayerInfo>) in.readObject();
            in.close();
        }
    }catch (Exception e){
        isItNull =true;
        initializeHashMap();
    }

    }

    private void getInfo(){

        System.out.println("Ok we are in get info so write your ID:-");
        int id = 10;
    }

    @SuppressWarnings("unchecked")
    private void createInfo()throws IOException{
//same here initialize ObjectOutputStreamin same method when I use it and close it then
        out =new ObjectOutputStream(new FileOutputStream(userData));

        System.out.println("Ok we are in create info so write your ID:-");
        ID =10;
        String scnS ="Mohammed";
        System.out.println("Write your name");
        map.put(ID,new PlayerInfo(scnS));
        temp.putAll(map);

        System.out.println("Saving....");
        out.writeObject(temp);
        out.close();
    }

    public PlayerInfo(String name){
        this.name =name;
    }


}

但这会抛出 EFOException


import java.io.*;
import java.util.HashMap;

public class PlayerInfo implements Serializable {

    ObjectOutputStream out;
    ObjectInputStream in;
    File userData =new File("path.ser");
    HashMap map ;
    HashMap temp;
    private Integer ID;
    String name ;
    boolean isItNull =false;

    public static void main(String[] args) {

        new PlayerInfo();

    }
    PlayerInfo(){
        try {
        openTheOutPutObjectStreamer();
        openTheInPutObjectStreamer();
        initializeHashMap();

        }catch (Exception e){
            e.printStackTrace();
        }

    }
//here I initialize it in separated method 
    private void openTheOutPutObjectStreamer()throws IOException{
        out =new ObjectOutputStream(new FileOutputStream(userData));

    }
//same here I initialize it in separated method 

    private void openTheInPutObjectStreamer()throws IOException{

        in =new ObjectInputStream(new FileInputStream(userData));

    }

    @SuppressWarnings("unchecked")
    private void initializeHashMap(){

    try {

        if (isItNull){
            temp =new HashMap<Integer,PlayerInfo>();

        }else {
            map =new HashMap<Integer,PlayerInfo>();
            temp = (HashMap<Integer, PlayerInfo>) in.readObject();
            in.close();
        }
    }catch (Exception e){
        isItNull =true;
        initializeHashMap();
    }

    }

    @SuppressWarnings("unchecked")
    private void createInfo()throws IOException{

        System.out.println("Ok we are in create info so write your ID:-");
        ID =10;
        String scnS ="Mohammed";
        System.out.println("Write your name");
        map.put(ID,new PlayerInfo(scnS));
        temp.putAll(map);

        System.out.println("Saving....");
        out.writeObject(temp);
        out.close();
    }

    public PlayerInfo(String name){
        this.name =name;
    }

}

如果你看到它,区别只是将对象输入/输出分离到一个方法并调用它们 我很抱歉我是这个网站的新手 我对 IO 不太了解,但似乎无法将其分离为方法并调用它?

【问题讨论】:

  • 您在哪一行得到异常?完整的错误是什么?

标签: java java-io objectinputstream objectoutputstream


【解决方案1】:

问题在于,在您的第一个代码中,您(正确地)打开一个输入流使用它,然后在对同一个文件执行任何其他操作之前关闭它,但在您的第二个代码版本中,您之前也打开了同一个文件上的输出流读取它并且输出流将标记(读取或写入的位置)放在文件末尾,因此当您使用输入流时,您会收到文件结束错误。

将您的代码更改为此应该可以工作

openTheInPutObjectStreamer();
initializeHashMap();
openTheOutPutObjectStreamer();

【讨论】:

  • 所以不能同时打开 InputObjectStreamer 和 OutputObjectStreamer?
  • 不,不在同一个文件上。可能还有其他 IO 流类可供您使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 2014-07-22
  • 2014-01-05
  • 2015-05-30
相关资源
最近更新 更多