我需要在方法 openLog 之外声明 PrintWriter 以便我可以从多个方法访问它,因为这样我只能在一个方法内访问 PrintWriter,但是我不能从其他方法访问它!
package com.donemanuel.DSDK;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LogKit {
void openLog() throws IOException{
Date ltm = new Date( );
SimpleDateFormat lt = new SimpleDateFormat ("'['dd.MM hh:mm:ss a']: '");
final String logtm = lt.format(ltm);
PrintWriter logd = new PrintWriter("res/LOGTIME_"+logtm, "UTF-8");
String prefix = "[Logger]:";
logd.println(prefix + "DSDK Logger opened!");
logd.println("----------xXx----------");
logd.flush();
}
void custommessage(String logmsg){
logd.println(logmsg); //I want to print custom messages with my API, but log is declared in another void so thats the problem.
//If i would declare logd (printwriter) outside a void it would give me an error!
}
}