我正在创建一个将错误日志写入文件的程序,但是当我请求保存该文件时,什么也没有发生(甚至连异常都没有)。我做错了什么?
“保存”按钮actionListener:
public void actionPerformed(ActionEvent arg0) {
String savePath = getSavePath();
try {
saveFile(savePath);
} catch (IOException e) {
e.printStackTrace();
}
}和三种文件方法:
private String getSavePath() {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.showOpenDialog(this);
return fc.getSelectedFile().getAbsolutePath();
}
private void saveFile(String path) throws IOException {
File outFile = createFile(path);
FileWriter out = null;
out = new FileWriter(outFile);
out.write("Hey");
out.close();
}
private File createFile(String path) {
String fileName = getLogFileName(path);
while (new File(fileName).exists()) {
fileCounter++;
fileName = getLogFileName(path);
}
File outFile = new File(fileName);
try {
outFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return outFile;
}
private String getLogFileName(String path) {
return "enchantcalc_err_log_" + fileCounter + ".txt";
}发布于 2013-04-21 04:55:47
您的getLogFileName(...)函数不会对您提供给它的路径做任何事情。因此,您尝试仅将文件写入enchantcalc_err_log_#.txt (没有实际路径)。试着这样做:
private String getLogFileName(String path) {
return path + "enchantcalc_err_log_" + fileCounter + ".txt";
}发布于 2013-04-21 05:58:14
您可能只是找不到该文件。
在saveFile的末尾,尝试这样做: After
out.close();像这样画一条线:
out.close();
System.out.println("File saved to: "+outFile.getAbsolutePath());然后,您将获得保存它的神秘路径。
https://stackoverflow.com/questions/16124947
复制相似问题