开发者

Write object to file not working

开发者 https://www.devze.com 2023-03-03 01:23 出处:网络
I have the following code: public static void logMessage(Object msg) { if (outputStream != null) { try {

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 st开发者_运维知识库opLogging() {
    try {
        if(outputStream != null){
            outputStream.close();
        }
    } catch (IOException e) {
        ErrorPopUp.setMessage(e.toString()).setVisible(true);
    }

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消