将对象写入文件不起作用

发布于 2024-11-05 03:10:37 字数 1917 浏览 4 评论 0原文

我有以下代码:

public static void logMessage(Object msg) {
    if (outputStream != null) {
        try {
            outputStream.writeObject(msg);
            outputStream.flush();
        } catch (IOException e) {
            ErrorPopUp.setMessage( e.toString()).setVisible(true);
        }
    } else {
        if ((logDirectory != null) && (writeLogName != null)) {
            try {
                setWriteFileName(writeLogName);
                outputStream.writeObject(msg);
                outputStream.flush();
            } catch (IOException e) {
                ErrorPopUp.setMessage( e.toString()).setVisible(true);
            }
        } else {
            ErrorPopUp.setMessage("Log file name not set!").setVisible(true);
        }
    }
}

使用

private static boolean setWriteFileName(String name)
throws IOException {
    writeLogName = name;

    if (logDirectory != null) {
        try {
            writeLogFile = new File(name);
            writeLogFile.createNewFile();
            outputStream = new ObjectOutputStream(
                    new BufferedOutputStream(
                    new FileOutputStream(
                    writeLogFile)));

            return true;
        } catch (IOException e) {
            throw e;
        }
    } else {
        ErrorPopUp.setMessage("Log directory name not set!").setVisible(true);
        return false;
    }
}

此代码,文件会像我期望的那样创建,但不会写入任何内容。
我很确定我错过了一些明显的东西,因为我对代码的关注太久了;)
我真的很感激一些建议。


编辑:

public static void stopLogging() {
    try {
        if(outputStream != null){
            outputStream.close();
        }
    } catch (IOException e) {
        ErrorPopUp.setMessage(e.toString()).setVisible(true);
    }

    if(writeLogFile != null){
        writeLogFile.setReadOnly();
    }
    outputStream = null;
}

I have the following code:

public static void logMessage(Object msg) {
    if (outputStream != null) {
        try {
            outputStream.writeObject(msg);
            outputStream.flush();
        } catch (IOException e) {
            ErrorPopUp.setMessage( e.toString()).setVisible(true);
        }
    } else {
        if ((logDirectory != null) && (writeLogName != null)) {
            try {
                setWriteFileName(writeLogName);
                outputStream.writeObject(msg);
                outputStream.flush();
            } catch (IOException e) {
                ErrorPopUp.setMessage( e.toString()).setVisible(true);
            }
        } else {
            ErrorPopUp.setMessage("Log file name not set!").setVisible(true);
        }
    }
}

and

private static boolean setWriteFileName(String name)
throws IOException {
    writeLogName = name;

    if (logDirectory != null) {
        try {
            writeLogFile = new File(name);
            writeLogFile.createNewFile();
            outputStream = new ObjectOutputStream(
                    new BufferedOutputStream(
                    new FileOutputStream(
                    writeLogFile)));

            return true;
        } catch (IOException e) {
            throw e;
        }
    } else {
        ErrorPopUp.setMessage("Log directory name not set!").setVisible(true);
        return false;
    }
}

Using this, the File gets created like I expect it to do, but nothing gets written into it.
I'm pretty sure it's something obvious that I'm missing because I'm looking to long at the code ;)
I really would appreciate some advise.


EDIT:

public static void stopLogging() {
    try {
        if(outputStream != null){
            outputStream.close();
        }
    } catch (IOException e) {
        ErrorPopUp.setMessage(e.toString()).setVisible(true);
    }

    if(writeLogFile != null){
        writeLogFile.setReadOnly();
    }
    outputStream = null;
}

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

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

发布评论

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

评论(1

山人契 2024-11-12 03:10:37

这是一个(静态)陷阱!我敢打赌 ObjectOutputStream outputStream 也是静态的!

It is a (static) TRAP! I bet ObjectOutputStream outputStream is static too!

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