简体   繁体   English

java.io.FileNotFoundException:无法找到我的文件?

[英]java.io.FileNotFoundException: It is not able to locate my file?

I am trying to implement a text file and a Bufferedreader in my Android Studio project but it is saying that the file cannot be found. 我正在尝试在我的Android Studio项目中实现一个文本文件和一个Bufferedreader,但这是说找不到该文件。 screenshot . 屏幕截图 Help? 救命?

Android Studio says that there is a possibility of FileNotFoundException . Android Studio表示存在FileNotFoundException的可能性。 So, you have to surround the error statement with try and catch block. 因此,您必须在try和catch块周围包含错误语句。

try {
    BufferedReader latQ = new BufferedReader(new FileReader("LatinQuotes.txt"));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Hope that helps. 希望能有所帮助。

As others have said, you need to surround the FileReader constructor with a try...catch block for FileNotFoundException. 就像其他人所说的那样,您需要在FileReader构造函数周围使用try ... catch块来包围FileNotFoundException。 The reason for this is that the FileReader constructor you are using throws a checked exception. 这样做的原因是,您使用的FileReader构造函数将引发一个已检查的异常。 What that means is that the constructor's method signature declares that it "throws FileNotFoundException". 这意味着构造函数的方法签名声明其“引发FileNotFoundException”。 This means that anyone that uses the constructor needs to "check" for that exception, or catch it. 这意味着使用构造函数的任何人都需要“检查”该异常,或者捕获该异常。 You should look into the concept of checked exceptions in Java. 您应该研究Java中检查异常的概念。

The exception appears when a file cannot be created, or the file exists but its permissions are insufficient for changing its content. 当无法创建文件或文件存在但其权限不足以更改其内容时,将出现异常。

Doing try/catch might work. 尝试/捕获可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM