【问题标题】:Read And Write Obj in Java From File . EOFException [duplicate]从文件中读取和写入 Java 中的 Obj。 EOFException [重复]
【发布时间】:2017-08-30 20:09:30
【问题描述】:

我正在尝试在文件上写入一个对象,然后从同一个文件中读取它 我从一个简单的文本文件中填充了一个对象数组列表。 当我尝试写入文件时出现 EOFException

public class CaricaTestoScriviOggetti_Copia {

    public static void main(String[] args) {
        File fileIn = new File("src/Lista.txt");
        Scanner input = null;
        try {
            input = new Scanner(fileIn);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ArrayList<Persona> persone = new ArrayList<Persona>();
        ArrayList<Persona> maggiorenni = new ArrayList<Persona>();
        String nome = null;
        String cognome = null;
        int eta = 0;
        while (input.hasNext()) {
            nome = input.next();
            cognome = input.next();
            eta = input.nextInt();
            if (input.hasNext("\n"))
                input.nextLine();
            persone.add(new Persona(nome, cognome, eta));
        }

        for (Persona p : persone) {
            System.out.println(p);
            if (p.getEta() > 17)
                maggiorenni.add(p);
        }

        input.close();

        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("src/ListaObj.dat");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            oos = new ObjectOutputStream(fos);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for (Persona p : maggiorenni) {
            try {
                oos.writeObject(p);
                oos.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // END FOR

        try {
            fos.close();
            oos.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        FileOutputStream fos2 = null;
        ObjectOutputStream oos2 = null;
        try {
            fos2 = new FileOutputStream("src/Oggetto.dat");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            oos2 = new ObjectOutputStream(fos);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            oos2.writeObject(maggiorenni.get(1));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            fos2.close();
            oos2.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("src/Oggetto.dat");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            ois = new ObjectInputStream(fis);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Persona catched = null;

        try {
            catched = (Persona) ois.readObject();
        } catch (ClassNotFoundException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // END FOR

        try {
            fis.close();
            ois.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        System.out.println("<------------Oggetto Letto------------>");
        System.out.println(catched);

        // END MAIN
    }
    // END CLASS
}

【问题讨论】:

  • '我在尝试写入文件时遇到 EOFException。'不,你没有。当您尝试文件读取时,您有一个EOFException,原因是文件in 中没有任何内容。注意不要写这样的代码。依赖于先前try 块中代码成功的代码必须在该try 块内。
  • 抱歉,我发布了错误的代码部分。你认为我必须删除这篇文章并用正确的代码部分写一篇文章?
  • 这是给我这个错误的代码
  • 您应该编辑您的问题。删除错误的代码并输入正确的代码。

标签: java serialization objectinputstream objectoutputstream


【解决方案1】:
p = (Persona) ois.readObject();
k++;
while (p != null) {
    persone.add(p);
    p = (Persona) ois.readObject();
}

您的读取循环无效。 readObject() 在流结束时不会返回 null,因此作为循环终止条件进行测试是没有意义的。

for (;;) {
    try {
        p = (Persona) ois.readObject();
        k++;
        persone.add(p);
    } catch (EOFException exc) {
        break;
    }
}

【讨论】:

    猜你喜欢
    • 2012-05-27
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多