不断遇到FileNotFoundException(日食)

时间:2019-01-14 00:49:34

标签: java eclipse

我正在尝试从名为gift1.in的文件中获取输入,但是我不断收到文件未找到错误。有任何想法吗? enter image description here

2 个答案:

答案 0 :(得分:0)

屏幕截图中的错误是未处理的异常编译器错误。

FileNotFoundException是受检查的异常,因为它源自IOException

如果未捕获或抛出异常,则检查后的异常在编译时将始终生成编译器错误。

throws FileNotFoundException中添加main或将代码将其扔到try/catch块中。

public static void main(String args[]) throws FileNotFoundException

Scanner input = null;
try {
  input = new Scanner(f);
}
catch {
  System.Out.Print("File not found");
  return 1;
}

该文件还需要位于资源文件夹中,构建脚本才能复制到目标文件夹中以运行。

我不确定Eclipse,但是我看到的标准是资源文件夹位于src/main/resources中。

答案 1 :(得分:0)

我现在没有日食,但问题可能出在路径上。试试这个

File f = new File(System.getProperty("user.dir") + "gift1.in");

File f = new File("[/absolute/path/to/file]");