2012-06-09 49 views
0

我有以下方法将数组写入文本文件。如果给出了现有的文本文件,那么它可以正常工作,但是如果一个不存在的文件被给出,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); 
    } 
} 

回答

0

FileWriter javadoc

从构造文档引用:

抛出: IOException - 如果指定文件存在,但它是一个目录,而不是一个常规文件,不存在,但不能被创建,或者无法打开任何其他原因

如果您传递一个不存在的文件名,但是在您有权写入的位置是一个合法的文件名,它只会创建该文件。

你的代码实际上确实到达了catch块,如果你将它传递给一个目录(有点奇怪,它在这种情况下为我记录FileNotFoundException而不是记录的IOException)。

要检查文件是否存在,请参阅File javadoc

+0

啊,这是有道理的。 – JKJones

2

如果文件不存在,您不能写入它,在您的catch块中尝试将错误写入不存在的文件。另外,我认为你在这里只需要1个catch块,并且注意if语句块之一是空的。

试试这个:

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) 
{ 
    System.err.println(e1.getMessage()); 
} 
catch (IOException e) 
{ 
    System.err.println(e.getMessage()); 
} 
+0

在我的catch块IM再次调用该方法,使用户可以尝试进入一个新的文本文件(该getInput方法提示用户输入,应该提到) – JKJones

+0

在任何情况下,无论我放在catch块中的任何代码都不会运行 – JKJones

+0

如何确定catch块中的代码根本不运行? –

0

试试这个版本,并发送堆栈跟踪,当你得到的异常:

public static List<String> splitByLength(String filename, int length) { 
    List<String> splitWords = new ArrayList<String>(); 

    for (int i = 0; i < str.length(); i += length) { 
     splitWords 
       .add(str.substring(i, Math.min(str.length(), i + length))); 
    } 
    return splitWords; 
} 

private static void openFileWriter(String prompt, boolean append, 
     int wordsperline, String[] story) { 
    PrintWriter save = null; 
    try { 
     save = new PrintWriter(new FileWriter("c:\\test.txt", append)); 
     wordsperline = 0; 
     save.println(""); 
     save.println(""); 
     save.println("Story start"); 
     for (int x = 0; x <= story.length - 1; x++) { 
      if (story[x] != null) { 
       List<String> splitWords = splitByLength(story[x], 21); 
       for (String line : splitWords) { 
        save.println(line); 
       } 
      } 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (save != null) { 
      save.close(); 
     } 
    } 
}