簡體   English   中英

即使存在throws語句,也接收FileNotFoundException

[英]receiving FileNotFoundException even though throws statement is present

我編寫了以下代碼,該代碼將隨機生成的字符列表打印到文件中,然后從文件中讀取它們,然后使用異或將其加密,然后再次打印。 問題是,即使我將它放在throws語句中,我仍然收到FileNotFoundException。

/**
 * @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)收到了類似Exception in thread "main" java.io.FileNotFoundException: (No such file or directory) 我認為可以通過使用throws FileNotFoundException語句來防止此錯誤。

我已經在這里查看了Oracle Java教程

我正在誠實地嘗試,但只是沒有點擊我。 我只用過try-catch語句來捕獲這樣的異常。 這是我每次嘗試保存以前未創建的文件時需要在這里執行的嗎?

方法簽名中的throws子句僅表示“我知道此方法會引發此異常,並且在此處未捕獲此異常,因此調用方應捕獲該異常。”

在您的示例中,您只是在欺騙編譯器,以便它不會抱怨未捕獲的異常。

通過添加FileNotFoundException,您正在聲明該函數可能會引發此異常,但是您並未添加任何將捕獲並抑制該異常的內容。

我會將您的代碼包裝到一個函數/類中,然后在您的主代碼塊中添加一個try塊來調用您的函數/類,並為FileNotFoundException添加catch塊。

throws子句不能避免引發異常。 實際上,它只是將方法聲明為拋出此類異常,以便其他調用它的方法可以相應地對其進行處理。

如果要抑制異常,則必須在語句周圍加上try-catch語句。 但是,禁止例外不是一個好習慣。 您應該正確對待它們,並向您的用戶顯示兼容消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM