【发布时间】:2018-10-25 13:57:04
【问题描述】:
我正在尝试读取整数的输入,例如
17
100
19
18
在 .txt 文件上,但我总是收到 FileNotFoundException。它会输出结果
0000
如果我运行下面的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class umm {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(new File("huhu.txt"));
int [] tall = new int [100];
int i = 0;
while(scanner.hasNextInt())
{
tall[i++] = scanner.nextInt();
System.out.print(tall[i]);
}
scanner.close();
}
}
如果我在 .txt 文件中添加整数,这样它将有 6 个这样的整数
17
100
19
18
2
5
它会输出
000000
这不是说文件存在并且可以访问吗?但是为什么老是说FileNotFound呢?
【问题讨论】:
-
使用文件的完整路径而不仅仅是huhu.txt
-
好吧。因为找不到文件。确保在正确的路径中找到文件。您可以在执行期间打印当前路径,例如
System.out.println(Paths.get(".").toAbsolutePath());
标签: java eclipse filenotfoundexception