我正在做一個記錄器類,並且我將代碼從println語句切換到做一個printwriter,但是,出現了問題,我找不出什麼。有人可以幫忙嗎?我知道問題出在Log方法的某個地方,但我不知道我能做些什麼來解決它。與我的記錄器printwriter問題
package com.company;
import java.io.PrintWriter;
enum MessageLevel {FatalError, LesserError, Warning, Info}
public class Logger
{
private static PrintWriter writer;
public boolean Initialize (String logfilename)
{
try
{
writer = new PrintWriter("logfile.log");
}
catch (java.io.FileNotFoundException e)
{
System.err.println("Your file " + logfilename +" is unable to open for write.\n");
}
writer.write("Hello World! \n");
writer.flush();
writer.close();
return true;
}
private static void WriteLog (String msg)
{
writer.write(msg);
}
private static String GetLevelString (MessageLevel level)
{
switch (level)
{
case FatalError:
return "Fatal Error: ";
case LesserError:
return "Lesser Error: ";
case Warning:
return "Warning: ";
case Info:
return "Info: ";
}
return "programmer error: missing level";
}
public static void Log (MessageLevel level, String msg)
{
writer.write(WriteLog(GetLevelString(level) + msg));
}
到底出了什麼問題?你是否收到錯誤信息? – Svea
你能告訴我們什麼是錯的嗎?或者你期待的是什麼? – Rafael