我正在使用PrintWriter类将文本从服务器写入文件。我的程序正在创建文件,但没有输出。有人可以告诉我这段代码的错误吗?欢呼声。
public class WriteFile {
PrintWriter logFileWriter;
public WriteFile() {
}
void startLogFile(String logName) {
File file = new File(logName);
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
logFileWriter = new PrintWriter(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void printToFile(String text) {
logFileWriter.printf(text + "\n");
}
void closeLogFile() {
logFileWriter.close();
logFileWriter = null;
}
//} remove this extra bracket which caused building error
}
答案 0 :(得分:0)
您可能没有在代码中调用closeLogFile()。设置一个断点来仔细检查。
答案 1 :(得分:0)
编辑和测试代码块后,我可以输出文本。步骤包括startLogFile,printToFile,closeLogFile。你有没有忘记在某处调用closeLogFile?
WriteFile writeFile = new WriteFile();
writeFile.startLogFile("test");
writeFile.printToFile("hahaha");
writeFile.closeLogFile();