将对象写入文件不起作用
我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个(静态)陷阱!我敢打赌
ObjectOutputStream outputStream
也是静态的!It is a (static) TRAP! I bet
ObjectOutputStream outputStream
is static too!