Java 不允许使用文件未找到异常,但这是必需的
因此,我遇到了一个奇怪的问题,我试图使用try-catch,但例外情况是它不允许。我将尽可能清楚地描述这个问题 一些背景: 好的,任务是基本上允许用户写一些文件,也可以读取它们。所以我在我的代码中调用一个以前生成的文件。用户可以键入他们希望调用的文件,因此当然,此异常是强制性的。但是错误表明从未抛出异常,但是当我运行代码时,确实会抛出异常。让我给你看一些代码Java 不允许使用文件未找到异常,但这是必需的,java,try-catch,filenotfoundexception,Java,Try Catch,Filenotfoundexception,因此,我遇到了一个奇怪的问题,我试图使用try-catch,但例外情况是它不允许。我将尽可能清楚地描述这个问题 一些背景: 好的,任务是基本上允许用户写一些文件,也可以读取它们。所以我在我的代码中调用一个以前生成的文件。用户可以键入他们希望调用的文件,因此当然,此异常是强制性的。但是错误表明从未抛出异常,但是当我运行代码时,确实会抛出异常。让我给你看一些代码 `while(error){ try{ respo
`while(error){
try{
response = scan.nextLine();
error = false;
}catch(FileNotFoundException e){
System.out.println("\nERROR: ");
error = true;
}
}`
这是我写的代码。现在,我的输出运行时没有尝试捕捉
这是我的错误:
主线程java.lang中出现异常。错误:未解析编译
问题:FileNotFoundException的捕获块不可访问。这
从不从try语句体抛出异常
位于fileLab2.MemoMaker.mainMemoMaker.java:126
希望有人能帮助解决这个问题。我只是为了这个问题做了这个解释,老实说,这很烦人。
请询问您是否需要任何澄清
以下是我的整个计划:
/*
* tslaurenx
* File reader lab 2
* OOP
* 11/20/15
* Lab 8b
*
*/
package fileLab2;
import java.util.*
import java.io.*;
public class MemoMaker {
public static void main(String[] args) throws IOException {
int choice = 0;
String name = "", response = "", memo = "";
boolean running = true, error = true;
String record = "";
System.out.print("Hello, and welcome to the Memo Maker 2015. ");
File memoTitles = new File("Memo Titles.txt");
PrintWriter writeTitles = new PrintWriter(memoTitles);
Scanner scan = new Scanner(System.in);
while(running){
while(error){
System.out.println("\nPlease select an option.");
System.out.println("\n(1) Create a Memo\n(2) View an Old Memo\n(3) Directions for Overwriting\n(4) Exit");
try{
choice = scan.nextInt();
if(choice >4 || choice <1){
System.out.println("\nERROR: Do you see that number there? Okay then... try again.\n");
error = true;
}//close if
else error = false;
}catch(InputMismatchException e){
scan.nextLine();
System.out.println("\nERROR: You need to enter the NUMBER next to your option.\n");
error = true;
} //close catch
}//close try while
error = true;
switch(choice){
case 1:
//create a file
Scanner key = new Scanner(System.in);
System.out.println("Enter the name of your memo.");
name = key.nextLine();
File file = new File(name + ".txt");
record = record + "\n" +file.getName();
writeTitles.print(record);
writeTitles.close();
PrintWriter write = new PrintWriter(file);
System.out.println("\nOkay, now write your memo: ");
memo = key.nextLine();
write.println(memo);
write.close();
System.out.println("\nYour memo has been created.");
break;
case 2:
//view an old file
Scanner readTitles = new Scanner(memoTitles);
Scanner enter = new Scanner(System.in);
if(readTitles.hasNext() == false){
System.out.println("There are no files available. Begin with making a memo.");
running = true;
break;
}//close if
else{
//view a file
System.out.println("Here are your available memos. \nEXACTLY type the name of the memo that you wish to view.\n\n");
System.out.println(record);
while(error){
try{
response = enter.nextLine();
error = false;
}catch(FileNotFoundException e){
System.out.println("\nERROR: ");
error = true;
}
}
error = true;
File trying = new File(response);
Scanner readMemo = new Scanner(trying);
System.out.println("\nHere is the message: ");
while(readMemo.hasNext() ){
String words = readMemo.nextLine();
System.out.println(words);
}//close while
while(error){
System.out.println("\n\nType 1 when you are finished viewing.");
int n = 0;
n = scan.nextInt();
if(n==1){
error = false;
}
else error = true;
}
readMemo.close();
}//close else
break;
case 3:
System.out.println("\nGo to Create a Memo, and type in the name of the file you wish to overwrite.\n");
break;
case 4:
Scanner ans = new Scanner(System.in);
int a = 0;
System.out.println("Are you sure you wish to exit? All of your memos will be lost.");
System.out.println("1. Yes\n2. No");
while (error){
try{
a = ans.nextInt();
if(a != 1 && a !=2){
System.out.println("\nERROR: Please enter a valid option.");
error = true;
}//close if
else error = false;
}catch(InputMismatchException e){
System.out.println("\nERROR: Please enter a valid option.");
error = true;
}//close catch
} //close try while
error = true;
if (a == 1){
System.out.println("Until next time!");
running = false;
}// close if
else {
running = true;
}
}//close switch
}//close running
scan.close();
writeTitles.close();
}//close main
}//close class
这条线,
File trying = new File(response);
正如您在下面的JavaDoc中所看到的,即使不会引发任何异常,也应该优先考虑在您的try…catch中包含该异常,并且使用一个应该包含在try块中的异常,因为它是引发异常的新FileInputStream构造函数。在尝试高效读取文件时,也不要使用扫描仪
以下是一个有用的链接:
public FileString路径名通过转换
将给定的路径名字符串转换为抽象路径名。如果给定
字符串是空字符串,则结果是空摘要
路径名
这意味着它不会检查文件是否存在
解决方案
请发布完整代码。您应该更仔细地指定ile的正确路径,在该路径中检索dataRead:错误表示从未从try语句体引发此异常。这是真的。抛出异常是对的,但它是从try语句体外部抛出的。@tslaurenx不客气。别忘了上传:DWhere新文件。。。打电话与此毫无关系。它不会抛出任何异常。@EJP这就是我添加它必须与FileInputStream构造函数一起使用的原因。到底为什么要投否决票?我只是改变了句子。@YassinHajaj我很想,但不幸的是我的声誉不够高。一旦达到15,它应该会改变。无论如何谢谢你!你的回答还是以一个错误的陈述开始。你的补充是你答案中唯一正确的部分。没有人会阻止你修正它,并在你做的时候改进语法和标点符号。你没有任何证据表明是谁否决了这个答案。
FileInputStream fis = null;
try{
fis = new FileInputStream(new File(enter.nextLine()));
error = false;
} catch (FileNotFoundException e){
// Blabla
}