当我在 IntelliJ 中将文件传递给 FileInputStream 构造函数时,它会抛出 FileNotFoundException
我正在尝试在 IntelliJ 中创建一个 FileInputStream 对象,但是当我通过将文件传递给构造函数来实例化该对象时,我在下面收到一条错误消息,指出存在未处理的 FileNotFoundException。这甚至阻止了我构建我的项目。这是我正在使用的代码:
import java.io.File;
import java.io.FileInputStream;
public class FileInputTest {
public void Test() {
File newFile = new File("RandomFile.txt");
FileInputStream newStream = new FileInputStream(newFile);
}
}
在我实例化 FileInputStream 对象的行下面有一条错误消息,指出存在未处理的 FileNotFoundException。即使 newFile 的实例化下没有错误,并且 RandomFile.txt 位于 IntelliJ 的工作目录中。因此该项目甚至无法构建,有人知道解决办法吗?
I'm trying to create a FileInputStream object in IntelliJ however when I instantiate the object by passing the file to the constructor, I get an error message underneath saying that there's an unhandled FileNotFoundException. This stops me from even building my project. Here's the code that I'm working with:
import java.io.File;
import java.io.FileInputStream;
public class FileInputTest {
public void Test() {
File newFile = new File("RandomFile.txt");
FileInputStream newStream = new FileInputStream(newFile);
}
}
There's an error message beneath the line where I instantiate the FileInputStream object stating that there's an unhandled FileNotFoundException. Even though there's no error beneath the instantiation of newFile and RandomFile.txt is in the working directory in IntelliJ. The project won't even build because of this, anyone know a fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当处理文件时,编译器会引发一个已检查的异常,在您的情况下它是 FileNotFoundException。要解决该问题:
另请尝试查看异常处理和 I/O 主题。
When working with files the compiler raises a checked Exception in your case it is FileNotFoundException. To fix the problem:
Also try to have a look at Exception Handling and I/O topics.