我有以下代码:
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;
}
答案 0 :(得分:1)
这是一个(静态)陷阱!我打赌ObjectOutputStream outputStream
也是静态的!