当我提取对象输入/输出流时出现EOFException

时间:2019-05-16 07:49:32

标签: java java-io objectinputstream objectoutputstream

我使用ObjectInput / Output初始化名为temp的哈希图,并将所有被称为map的哈希图的条目初始化为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不太了解,但似乎无法将其与方法分开并调用它?

1 个答案:

答案 0 :(得分:0)

问题在于,在您的第一个代码中(正确)打开输入流会先使用它,然后再关闭它,然后再对同一文件执行其他操作,但是在第二个代码版本中,您还会在相同的文件上打开输出流读完该输出流后,将标记(读取或写入的位置)放在文件的末尾,因此当您使用输入流时,会出现文件结束错误。

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

df1 = pd.DataFrame({'month': [1, 4, 7, 10], 'year': [2012, 2014, 2013, 2014], 'sale': [55, 40, 84, 31]})
df2 = pd.DataFrame({'month': [1, 10], 'year': [2012, 2014], 'sale': [55, 31]})
pd.concat([df1, df2]).drop_duplicates(keep=False)