[英]Why does file.exists() always return true if I don't throw FileNotFoundExcetion in the first place
public class Main {
final private static int MAX_RECORD_NUMBER = 20;
final private static int RECORD_LENGTH = 71;
public static void main(String[] args) throws IOException, FileNotFoundException{
Scanner input = new Scanner(System.in);
System.out.println("Please input the file location and name.");
String filepath = input.next();
File file = new File(filepath);
boolean isExisted = file.exists(); // Problem exists here if I delete FileNotFoundException
RandomAccessFile store = new RandomAccessFile(file, "rw");
if (!isExisted) {
String dummy = "Empty record ";
for (int i = 0; i < MAX_RECORD_NUMBER; i++) {
store.writeUTF(dummy);
}
}
我在这里遇到了小麻烦。 如果我不抛出FileNotFoundException,则file.exists()方法将始终返回true (即使文件确实不在该位置)。 在这种情况下,如果我没有在主方法的开头抛出FileNotFoundException,则无法将UTF写入该文件。 这肯定是一个愚蠢的问题,但我只想知道其背后的机制是什么。 API也无济于事。 我真的很感谢任何可以向我解释的人。
编辑:对不起,我之前不清楚。 我真正想问的是为什么.exists()方法总是返回true而不是false或其他意外错误?
您需要了解在哪里抛出该异常。 而这里是这一行:
RandomAccessFile store = new RandomAccessFile(file, "rw");
看一下FileNotFoundException的API,您会知道它在RandomAccessFile构造函数中按预期方式被抛出。
使用以下代码在没有RandomAccessFile的情况下测试您的逻辑:
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please input the file location and name.");
String filepath = input.next();
File file = new File(filepath);
if (file.exists()) System.out.println("EXISTS!"); else System.out.println("DOESN'T EXIST!");
}
}
现在问题的第一部分应该清楚了。 函数file.exists()
正常工作。 逻辑上的缺陷来自以下事实:RandomAccessFile构造函数能够创建文件,但并非总是如此。 这就是为什么您需要处理异常。 以下是构造函数的Java 8 API的摘录:
FileNotFoundException-如果模式为“ r”但给定的文件对象不表示现有的常规文件,或者模式以“ rw”开头但给定的文件对象不表示现有的可写常规文件和新的常规文件名称无法创建,或者在打开或创建文件时发生其他错误
因此,代码的另一部分可能看起来像这样:
try {
RandomAccessFile store = new RandomAccessFile(file, "rw");
String dummy = "Empty record ";
for (int i = 0; i < MAX_RECORD_NUMBER; i++) {
store.writeUTF(dummy);
}
} catch (FileNotFoundException e){
System.err.println("File cannot be created!");
}
但是,就异常服务而言,这还不够,因为您在此处使用了writeUTF(dummy)
函数。 并且此函数抛出:
IOException-如果发生I / O错误。 (如RandomAccessFile的API所述。)
因此,您需要服务IOException。 例如,将一个catch子句追加到现有的catch子句中:
catch (IOException e){
System.err.println("Couldn't write to file!");
}
或者,您可以跳过FileNotFoundException,因为它是IOException的子类,并且只有一个catch子句与IOException。 但是,正如您所看到的,很明显,它们代表两种不同的原因。
因此,最终您的课程如下所示:
import java.io.*;
import java.util.Scanner;
public class Main {
final private static int MAX_RECORD_NUMBER = 20;
final private static int RECORD_LENGTH = 71;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please input the file location and name.");
String filepath = input.next();
File file = new File(filepath);
//if (file.exists()) System.out.println("EXISTS!"); else System.out.println("DOESN'T EXIST!");
try {
RandomAccessFile store = new RandomAccessFile(file, "rw");
String dummy = "Empty record ";
for (int i = 0; i < MAX_RECORD_NUMBER; i++) {
store.writeUTF(dummy);
}
} catch (FileNotFoundException e){
System.err.println("File cannot be created!");
} catch (IOException e){
System.err.println("Couldn't write to file!");
}
}
}
某些部分是不需要的。 分析我的解决方案,它应该可以澄清您的错误。 或要求我更多。 无论如何,这个答案有点冗长。
最后一点。 其他线程的人对类似的事情感到困惑。 您可以在右侧看到指向它们的链接。 简而言之,请使用文件的完整路径。 有关更多详细信息,请检查链接。
这直接来自Java API :
java.io.FileNotFoundException
This exception will be thrown by [...] when a file with the specified pathname does not exist.
这样你就可以包围
RandomAccessFile store = new RandomAccessFile(file, "rw");
使用如下所示的try-catch块:
Scanner input = new Scanner(System.in);
System.out.println("Please input the file location and name.");
String filepath = input.next();
File file = new File(filepath);
boolean isExisted = file.exists();
try{
RandomAccessFile store = new RandomAccessFile(file, "rw");
if (!isExisted) {
String dummy = "Empty record ";
for (int i = 0; i < MAX_RECORD_NUMBER; i++) {
store.writeUTF(dummy);
}
}
}catch (FileNotFoundException e){
System.err.println("File not found!");
e.printStackTrace();
}catch (IOException e){
System.err.println("Couldn't write to/read file!");
e.printStackTrace();
}
这样,如果出现问题(用户输入错误的路径/用户无权读取/写入文件),则可以处理错误,而不会导致程序崩溃。 有关try-catch块的更多信息,请参见此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.