即使存在throws语句,也会收到FileNotFoundException

时间:2014-03-14 01:18:19

标签: java exception-handling filenotfoundexception

我编写了以下代码,将随机生成的字符列表打印到文件中,然后从文件中读取它们,使用独占或加密它们,然后再次打印它们。问题是我收到了FileNotFoundException,即使我已将它放在throws语句中。

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException {


    //get integer mask from user input
    int mask = getMask();
    System.out.println("TEMP mask Value is: " + mask);

    //create 50 character random String and save to file
    String randomString = getRandomString(50);
    System.out.println("Original Random Character String: " + '\n' + randomString);

    //save 50 Char String from file
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Saving encrypted string...");
    System.out.print("Enter a file name: ");
    String fileName = keyboard.next();
    File outputFile = new File(fileName);
    saveFile(fileName, randomString);
    /*System.out.println("Saving Original Random Character string...");
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter a file name: ");
    String fileName = keyboard.next();
    File outputFile = new File(fileName);
    PrintWriter fileWriter = new PrintWriter(outputFile);
    fileWriter.println(randomString);

    fileWriter.close();//CLOSE OF FILEWRITER
    */


    //scan in from file
    Scanner file = new Scanner(outputFile);
    String inputString = file.nextLine();
    //print what was just scanned in from file
    System.out.print("Original random character string from the file" + 
            '\n' + inputString + '\n');

    //apply mask by convertig String to char using loop
    String maskedString = maskString(inputString, mask);
    System.out.print("Encrypted character string: " + '\n' + maskedString + '\n');
    /*String maskedString = "";
    for(int i = 0; i < inputString.length(); i++){
    char charMasked = (char)(((int) inputString.charAt(i))^mask);
    maskedString += charMasked;
    }//end of for loop
    System.out.print("Encrypted character string: " + '\n' + maskedString + '\n');
    */

    //save encrypted string
    System.out.println("Saving encrypted string...");
    System.out.print("Enter a file name: ");
    String encryptedFileName = keyboard.nextLine();
    saveFile(encryptedFileName, maskedString);





}//end of main method
/**
 * Preconditions: must call randomString method to get random 
 * characters
 * Postconditions: user will have a string of random characters of desired
 * length
 * @param count allows user to choose how many random characters to return
 */
public static String getRandomString(int count)throws FileNotFoundException{

    String listChars = "";
    for (int i = 0; i < count; i++) {
        char randomChar = (char) ((Math.random() * 255) + 32);/////////////////////getting less than 50 characters sometimes, possible control characters?
        listChars += randomChar;
    }//end of for loop
    return listChars;
}//end of randomString method

/**
 * Precondition: must call getMask method to get prompt user to enter the 
 * encryption mask that will be used to encrypt the file input
 * Postcondition: user has entered an integer string that will be used to 
 * encrypt the 50 char random String we will read from a file
 * @return output, which is the user entered integer value
 */

public static int getMask(){
    //Prompt user to enter integer mask
    System.out.print("Enter the encryption mask: ");
    Scanner keyboard = new Scanner(System.in);
    int output = keyboard.nextInt();
    return output;
}//end of getMask method

public static void saveFile(String fileName, String toBePrinted)throws FileNotFoundException{

    File outputFile = new File(fileName);
    PrintWriter fileWriter = new PrintWriter(outputFile);
    fileWriter.println(toBePrinted);

    fileWriter.close();//CLOSE OF FILEWRITER
}//end of saveFile method

public static String maskString(String inputString, int mask){
String maskedString = "";
    for(int i = 0; i < inputString.length(); i++){
    char charMasked = (char)(((int) inputString.charAt(i))^mask);
    maskedString += charMasked;
    }//end of for loop
    return maskedString;
}//end of maskString method

执行我的代码后,我会收到Exception in thread "main" java.io.FileNotFoundException: (No such file or directory)行的内容。我认为使用throws FileNotFoundException语句会阻止此错误。

我查看了Oracle Java教程here

我老老实实地尝试,但它只是没有点击我。我只使用try-catch语句来捕获这样的异常。这是我每次尝试保存以前没有创建过的文件时我需要做什么的?

3 个答案:

答案 0 :(得分:1)

方法签名中的

throws子句仅表示“我知道此方法可以抛出此异常,我不会在此处捕获它,因此调用者应该捕获它。”

在您的示例中,您只是欺骗编译器,以便它不会抱怨未捕获的异常。

答案 1 :(得分:0)

通过添加FileNotFoundException,您声明该函数可能会抛出此异常,但您不会添加任何会捕获并抑制异常的内容。

我将你的代码包装到一个函数/类中,然后在你的主代码块中添加一个调用你的函数/类的try块,并为FileNotFoundException添加一个catch块。

答案 2 :(得分:0)

throws子句不能避免抛出异常。实际上,它只是将方法声明为抛出此异常,以便调用它的其他方法可以相应地处理它。

如果要禁止例外,则必须使用try-catch语句包围语句。但是,抑制异常并不是一种好的做法。您应该正确对待它们并向用户显示兼容的消息。