2014-03-14 69 views
0

我寫了下面的代碼打印出隨機生成的字符列表的文件,然後從文件中讀取它們,使用專用或對它們進行加密,然後再打印它們。問題是即使我將它放在一個throws語句中,我也收到FileNotFoundException。即使拋出聲明接收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)的行。我認爲這個錯誤會被我使用throws FileNotFoundException聲明阻止。

我已經查閱了甲骨文的Java教程here

我誠實地努力,但它只是不跟我一下。我只使用try-catch語句來捕獲這樣的異常。這是我每次嘗試保存一個以前沒有創建的文件時需要做的嗎?

+0

'嘗試saveFile(fileName,randomString); } 趕上(FileNotFoundException異常五){};' – FluffyKittens

+0

結束語我的方法內我的主要一個try-catch語句是我已經想出這麼遠,我意識到我的自定義方法,saveFile的()拋出異常,和在這種情況下,它將被我的主要方法中的try-catch語句捕獲。這是猶太人嗎?或者,還有更好的方法? – FluffyKittens

回答

2

throws子句中的方法簽名只意味着「我知道這個方法可以拋出這個異常,我不會在這裏捕獲它,所以調用者應該捕獲它。」

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

1

通過添加FileNotFoundException您聲明該函數可能會引發此異常,但您沒有添加任何可以捕獲並禁止該異常的東西。

我想包你的代碼放到一個函數/類,然後在你的主代碼塊添加一個try塊調用你的函數/類,並添加一個catch塊的FileNotFoundException異常。

1

throws子句不會避免拋出異常。實際上,它只是聲明方法拋出這樣的異常,以便其他方法調用它可以相應地處理它。

如果要抑制異常,必須圍繞着你的發言有try-catch聲明。但是,抑制例外不是一個好習慣。您應該正確處理它們並向用戶顯示兼容的消息。

相關問題