如何使用 try/catch 避免在 java 中抛出 FileNotFoundException
How to use try/catch to avoid throwing FileNotFoundException in java
我正在尝试从我的方法中输出一个文件扫描器对象。这是一项学校作业,我被特别指示不要抛出任何异常,而是使用 try/catch 代替。该分配要求命令行提示用户输入要扫描的文件。如果文件不存在,我们应该告诉用户,然后再次提示他们输入文件。如果文件确实存在,那么方法 returns 扫描文件的扫描仪对象。
我的代码有效,但不干净。它涉及2种方法。到目前为止,这是我的代码:
public static Scanner getInputScanner (Scanner console) {
File inputFile = null;
Scanner input = null;
try {
inputFile = getFile(inputFile, console);
input = new Scanner (inputFile);
return input;
} catch (FileNotFoundException e) {
try {
return input = new Scanner (getFile (inputFile, console));
} catch (FileNotFoundException f) {
System.out.println("An error has occured.");
return input;
}
}
}
public static File getFile (File inputFile, Scanner console) {
System.out.println("Enter input file: ");
inputFile = new File (console.nextLine());
while (!inputFile.exists()) {
System.out.println("File does not exist.");
System.out.print("Enter input file: ");
inputFile = new File (console.nextLine());
}
return inputFile;
}
代码的问题在于输出如下所示:
输入输入文件:
文件不存在。
输入输入文件:
然后等待用户输入。我不希望输出在最后一行之前有 2 行代码。
有人可以解释为什么我的代码输出这两行吗?
另外,是否有更简单的解决方案来获取输入文件而不抛出 FileNotFoundException?
谢谢!
如果我没理解错的话,
你的程序在你 运行 时输出这些行,
无论,
没有机会实际输入文件名。
Enter input file:
File does not exist.
然后程序再次询问您:
Enter input file:
你不想要上面的前两行,对吧?
例如,如果您收到的 Scanner console
中有未读的换行符,就会发生这种情况。
你还没有发布那部分代码,
所以很难说,但这是 Scanner
的常见问题。
在调用 getInputScanner
之前,
确保 Scanner console
可以使用,
没有未读的垃圾仍然缓冲在其中。
关于你问题的第二部分,
是的,这可以写得更简单更好,例如:
public static Scanner getInputScanner(Scanner console) {
try {
File inputFile = getExistingFile(console);
return new Scanner(inputFile);
} catch (FileNotFoundException e) {
throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
}
}
public static File getExistingFile(Scanner console) {
while (true) {
System.out.println("Enter input file: ");
File inputFile = new File(console.nextLine());
if (inputFile.exists()) {
return inputFile;
}
System.out.println("File does not exist.");
}
}
它在调用 getFile() 后立即执行下面的行。
System.out.print("Enter input file: ");
由于不存在文件,以下行继续执行:
同时 (!inputFile.exists()) {
System.out.println ("File does not exist.");
System.out.print("Enter input file: ");
您可以使用 throws() 而不是 try/catch,然后调用者会处理异常。
必须在获取用户输入之前通过插入 Scanner.nextLine() 来消耗从扫描仪携带的任何垃圾。最终代码如下所示:
public static Scanner getInputScanner(Scanner console) {
try {
File inputFile = getExistingFile(console);
return new Scanner(inputFile);
} catch (FileNotFoundException e) {
throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
}
}
public static File getExistingFile(Scanner console) {
while (true) {
console.nextLine();
System.out.println("Enter input file: ");
File inputFile = new File(console.nextLine());
if (inputFile.exists()) {
return inputFile;
}
System.out.println("File does not exist.");
}
}
我正在尝试从我的方法中输出一个文件扫描器对象。这是一项学校作业,我被特别指示不要抛出任何异常,而是使用 try/catch 代替。该分配要求命令行提示用户输入要扫描的文件。如果文件不存在,我们应该告诉用户,然后再次提示他们输入文件。如果文件确实存在,那么方法 returns 扫描文件的扫描仪对象。
我的代码有效,但不干净。它涉及2种方法。到目前为止,这是我的代码:
public static Scanner getInputScanner (Scanner console) {
File inputFile = null;
Scanner input = null;
try {
inputFile = getFile(inputFile, console);
input = new Scanner (inputFile);
return input;
} catch (FileNotFoundException e) {
try {
return input = new Scanner (getFile (inputFile, console));
} catch (FileNotFoundException f) {
System.out.println("An error has occured.");
return input;
}
}
}
public static File getFile (File inputFile, Scanner console) {
System.out.println("Enter input file: ");
inputFile = new File (console.nextLine());
while (!inputFile.exists()) {
System.out.println("File does not exist.");
System.out.print("Enter input file: ");
inputFile = new File (console.nextLine());
}
return inputFile;
}
代码的问题在于输出如下所示:
输入输入文件:
文件不存在。
输入输入文件:
然后等待用户输入。我不希望输出在最后一行之前有 2 行代码。
有人可以解释为什么我的代码输出这两行吗? 另外,是否有更简单的解决方案来获取输入文件而不抛出 FileNotFoundException?
谢谢!
如果我没理解错的话, 你的程序在你 运行 时输出这些行, 无论, 没有机会实际输入文件名。
Enter input file: File does not exist.
然后程序再次询问您:
Enter input file:
你不想要上面的前两行,对吧?
例如,如果您收到的 Scanner console
中有未读的换行符,就会发生这种情况。
你还没有发布那部分代码,
所以很难说,但这是 Scanner
的常见问题。
在调用 getInputScanner
之前,
确保 Scanner console
可以使用,
没有未读的垃圾仍然缓冲在其中。
关于你问题的第二部分, 是的,这可以写得更简单更好,例如:
public static Scanner getInputScanner(Scanner console) {
try {
File inputFile = getExistingFile(console);
return new Scanner(inputFile);
} catch (FileNotFoundException e) {
throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
}
}
public static File getExistingFile(Scanner console) {
while (true) {
System.out.println("Enter input file: ");
File inputFile = new File(console.nextLine());
if (inputFile.exists()) {
return inputFile;
}
System.out.println("File does not exist.");
}
}
它在调用 getFile() 后立即执行下面的行。
System.out.print("Enter input file: ");
由于不存在文件,以下行继续执行:
同时 (!inputFile.exists()) {
System.out.println ("File does not exist.");
System.out.print("Enter input file: ");
您可以使用 throws() 而不是 try/catch,然后调用者会处理异常。
必须在获取用户输入之前通过插入 Scanner.nextLine() 来消耗从扫描仪携带的任何垃圾。最终代码如下所示:
public static Scanner getInputScanner(Scanner console) {
try {
File inputFile = getExistingFile(console);
return new Scanner(inputFile);
} catch (FileNotFoundException e) {
throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
}
}
public static File getExistingFile(Scanner console) {
while (true) {
console.nextLine();
System.out.println("Enter input file: ");
File inputFile = new File(console.nextLine());
if (inputFile.exists()) {
return inputFile;
}
System.out.println("File does not exist.");
}
}