gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:16:56 24 4
gpt4 key购买 nike

我编写了以下代码,将随机生成的字符列表打印到文件中,然后从文件中读取它们,使用异或加密它们,然后再次打印它们。问题是我收到了 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

执行代码后,我收到类似于线程“main”java.io.FileNotFoundException中的异常:(没有这样的文件或目录)的内容。我认为使用 throws FileNotFoundException 语句可以防止此错误。

我已经查看了 Oracle Java 教程 here

老实说,我正在尝试,但它只是不适合我。我只使用过 try-catch 语句来捕获这样的异常。这是我每次尝试保存以前未创建的文件时都需要做的事情吗?

最佳答案

方法签名中的

throws 子句仅意味着“我知道该方法可以抛出此异常,但我没有在这里捕获它,因此调用者应该捕获它。”

在您的示例中,您只是欺骗编译器,使其不会提示未捕获的异常。

关于java - 即使存在 throws 语句,也会收到 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22394326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com