命令行 - FileNotFoundException

时间:2016-11-24 00:35:04

标签: java terminal try-catch command-line-arguments filenotfoundexception

我正在创建一个测试人员。

当我尝试

javac WordListsTester.java java WordListsTester a.txt

我认为它应该显示一条错误消息,因为该文件必须是“dictionary.txt”,但事实并非如此。

我应该修理什么?

public class WordListsTester {
public static Scanner input = new Scanner(System.in);

public static void main(String[] args) throws FileNotFoundException {

    WordLists scrabble = new WordLists("dictionary.txt");

    String[] containLetter = scrabble.containsLetter(3, 'a');
    output(containLetter, "containsLetter.txt");

    String[] wordsStarts = scrabble.startsWith(3, 'a');
    output(wordsStarts, "startsWith.txt");

    String[] wordLength = scrabble.lengthN(3);
    output(wordLength, "lengthN.txt");

    String[] multiLetter = scrabble.multiLetter(2, 'h');
    output(multiLetter, "multiLetter.txt");

    String[] vowelHeavy = scrabble.vowelHeavy(5, 2);
    output(vowelHeavy, "vowelHeavy.txt");

    input.close();

}

public static void output(String[] words, String fileName) {
    try {
        PrintWriter out = new PrintWriter(fileName);
        if (words.length == 0) {
            System.out.println("NO matched Words exist");
        }
        for (int i = 0; i < words.length; i++) {
            out.println(words[i]);
        }
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("Invalid file name.");
    }
}

}

1 个答案:

答案 0 :(得分:0)

WordLists scrabble = new WordLists(args[0]);

不确定这个程序到底做了什么。

不确定是否在WordLists类中检查文件名。

但是如果你想检查USER INPUT,你需要检查args [0]。