写入文件时 java 对象流的问题

发布于 2024-09-13 20:41:01 字数 1541 浏览 3 评论 0原文

我正在尝试将“HashMap”类型的对象写入文件&当我的程序再次运行时恢复它。但是当我尝试读取该对象并且未从文件中读取该对象时,我遇到了 EOFException。我使用flush() &当我为 FileOutputStream 编写对象时 close() 方法对象输出流。我还创建了 OutputStream 和我的文件的InputStream 一起。 这是我的代码:

DataOutputStream outToFile;
DataInputStream inFromFile;

ObjectOutputStream writeTableToFile;
ObjectInputStream readTableFromFile;
File tableFile;

public DNS(){
    try {
        tableFile = new File("table.txt");
        outToFile = new DataOutputStream(new FileOutputStream(tableFile) );
        writeTableToFile = new ObjectOutputStream(outToFile);

        inFromFile = new DataInputStream(new FileInputStream(tableFile));
        readTableFromFile = new ObjectInputStream(inFromFile);
        HashMap table2 = (HashMap) readTableFromFile.readObject();
        if (table2 == null)
            table=new HashMap(100);
        else
            table = table2;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(EOFException e){
        table=new HashMap(100);
    }
    catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

这是编写对象的代码:

            table.put(NameField.getText(), IPField.getText());
            try {
                //writeTableToFile.reset();
                writeTableToFile.writeObject(table);
                writeTableToFile.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

问候, 萨贾德

I am trying to write an Object of kind "HashMap" to a file & recover it when my program run again. But I faced with an EOFException when I try to read that object and the Object is not read from the file. I use the flush() & close() methods when I wrote the object for the FileOutputStream & ObjectOutputStream. Also I create OutputStream & InputStream together for my file.
here is my code:

DataOutputStream outToFile;
DataInputStream inFromFile;

ObjectOutputStream writeTableToFile;
ObjectInputStream readTableFromFile;
File tableFile;

public DNS(){
    try {
        tableFile = new File("table.txt");
        outToFile = new DataOutputStream(new FileOutputStream(tableFile) );
        writeTableToFile = new ObjectOutputStream(outToFile);

        inFromFile = new DataInputStream(new FileInputStream(tableFile));
        readTableFromFile = new ObjectInputStream(inFromFile);
        HashMap table2 = (HashMap) readTableFromFile.readObject();
        if (table2 == null)
            table=new HashMap(100);
        else
            table = table2;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(EOFException e){
        table=new HashMap(100);
    }
    catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

and here is code for writing object:

            table.put(NameField.getText(), IPField.getText());
            try {
                //writeTableToFile.reset();
                writeTableToFile.writeObject(table);
                writeTableToFile.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

Regards,
sajad

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

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

发布评论

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

评论(2

白云悠悠 2024-09-20 20:41:01

该文件似乎不完整。当我查看您的代码时,您正在创建文件 table.txt 并尝试随后立即读取它。

该构造函数:

new FileOutputStream(tableFile)

将覆盖该文件。如果你之后再读它,它将是空的(除了 OOS 的头信息)

The file seems to be incomplete. When I look at your code, you're creating the file table.txt and try to read it immediately afterwards.

This ctor:

new FileOutputStream(tableFile)

will overwrite the file. If your read it afterwards, it will be empty (except the header information from the OOS)

倥絔 2024-09-20 20:41:01

EOFException 表示文件不完整。所以它要么没有flush()ed,要么没有close()ed,或者异常被吞没在某个地方。

EOFException means that the file is incomplete. So it's either not flush()ed or not close()ed or an exception is swallowed somewhere.

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