Java 即使存在TROWS语句,仍接收FileNotFoundException
我编写了以下代码,将随机生成的字符列表打印到文件中,然后从文件中读取它们,使用异或对它们进行加密,然后再次打印它们。问题是,我收到了一个FileNotFoundException,尽管我已经将它放在了一个throws语句中Java 即使存在TROWS语句,仍接收FileNotFoundException,java,exception-handling,filenotfoundexception,Java,Exception Handling,Filenotfoundexception,我编写了以下代码,将随机生成的字符列表打印到文件中,然后从文件中读取它们,使用异或对它们进行加密,然后再次打印它们。问题是,我收到了一个FileNotFoundException,尽管我已经将它放在了一个throws语句中 /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { //get intege
/**
* @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
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发FileNotFoundException{
//从用户输入获取整数掩码
int mask=getMask();
System.out.println(“温度掩码值为:“+掩码”);
//创建50个字符的随机字符串并保存到文件
String randomString=getRandomString(50);
System.out.println(“原始随机字符串:”+'\n'+randomString);
//从文件中保存50个字符字符串
扫描仪键盘=新扫描仪(System.in);
System.out.println(“保存加密字符串…”);
System.out.print(“输入文件名:”);
字符串文件名=keyboard.next();
文件输出文件=新文件(文件名);
保存文件(文件名,随机字符串);
/*System.out.println(“保存原始随机字符串…”);
扫描仪键盘=新扫描仪(System.in);
System.out.print(“输入文件名:”);
字符串文件名=keyboard.next();
文件输出文件=新文件(文件名);
PrintWriter fileWriter=新的PrintWriter(输出文件);
fileWriter.println(随机字符串);
fileWriter.close();//关闭fileWriter
*/
//从文件中扫描
扫描仪文件=新扫描仪(输出文件);
String inputString=file.nextLine();
//打印刚从文件中扫描的内容
System.out.print(“文件中的原始随机字符串”+
'\n'+inputString+'\n');
//使用循环将convertig字符串的掩码应用于char
字符串掩码字符串=掩码字符串(inputString,掩码);
System.out.print(“加密字符串:++'\n'+maskedString++'\n');
/*字符串掩码字符串=”;
对于(int i=0;i
在执行我的代码时,我在线程“main”java.io.FileNotFoundException:(没有这样的文件或目录)中接收到类似于异常的内容。我认为使用throws-FileNotFoundException
语句可以防止这个错误
我已经复习了Oracle Java教程
老实说,我正在努力,但它只是不与我点击。我只使用过try-catch语句来捕获这样的异常。这就是我每次尝试保存以前未创建的文件时需要在此处执行的操作吗?方法签名中的throws
子句仅表示“我知道此方法可以引发此异常,但我无法在此处捕获它,因此调用方应捕获它。”
在您的示例中,您只是在欺骗编译器,使其不会抱怨未捕获的异常。通过添加FileNotFoundException,您声明的函数可能会引发此异常,但您没有添加任何捕获和抑制异常的内容
我将把代码包装成一个函数/类,然后在主代码块中添加一个调用函数/类的try块,并为FileNotFoundException添加一个catch块。throws
子句不能避免抛出异常。实际上,它只是将该方法声明为抛出这样的异常,以便其他调用它的方法可以相应地处理它
如果要抑制异常,必须用try-catch
语句包围语句。但是,禁止例外并不是一种好的做法。你应该