Java读取数字文件并将其打印出来

Bob*_*Bob 0 java

我有这个代码

Scanner scanner = new Scanner("hello.txt");
   while(scanner.hasNextInt()){
       int i = scanner.nextInt();
       System.out.println(i);
    }
Run Code Online (Sandbox Code Playgroud)

我在hello.txt中的数据值是

1 2 3 22 33 123

但是当我运行程序时,没有输出.有什么我不使用/代码行?

Ric*_*arn 5

Scanner您正在使用的构造函数带有一个字符串,可以从中读取值.这不是文件名.字符串中没有整数,hello.txt因此您没有输出.

如果要从调用的文件中读取hello.txt,请尝试

Scanner scanner = new Scanner(new File("hello.txt"));
Run Code Online (Sandbox Code Playgroud)