我有以下方法将数组写入文本文件。如果给出了现有的文本文件,那么它可以正常工作,但是如果一个不存在的文件被给出,try-catch将运行代码来重新启动该方法。我没有给出任何错误或任何东西,但catch块不会运行。我不认为我需要赶上IOException,但如果我不这样做,代码甚至不会运行。所以,有人知道我可以如何让这个工作?catch块为什么不运行?
编辑:忘了提及getInput方法提示用户输入。
private static void openFileWriter(String prompt, boolean append, int wordsperline, String[] story) {
try {
try {
save = new PrintWriter(new FileWriter(getInput(prompt), append));
wordsperline = 0;
save.println("");
save.println("");
save.println("Story start");
for (int x = 0; x <= story.length-1; x++) {
if (story[x] == null) {
} else {
if (wordsperline == 21) {
save.println(story[x]);
wordsperline = 0;
} else {
save.print(story[x]);
wordsperline++;
}
}
}
save.close();
} catch (FileNotFoundException e1) {
openFileWriter("File not found", append,wordsperline,story);
}
} catch (IOException e) {
// TODO Auto-generated catch block
openFileWriter("File not found", append,wordsperline,story);
}
}
啊,这是有道理的。 – JKJones