我寫了下面的代碼打印出隨機生成的字符列表的文件,然後從文件中讀取它們,使用專用或對它們進行加密,然後再打印它們。問題是即使我將它放在一個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語句來捕獲這樣的異常。這是我每次嘗試保存一個以前沒有創建的文件時需要做的嗎?
'嘗試saveFile(fileName,randomString); } 趕上(FileNotFoundException異常五){};' – FluffyKittens
結束語我的方法內我的主要一個try-catch語句是我已經想出這麼遠,我意識到我的自定義方法,saveFile的()拋出異常,和在這種情況下,它將被我的主要方法中的try-catch語句捕獲。這是猶太人嗎?或者,還有更好的方法? – FluffyKittens