未找到扫描仪类 java 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11385013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:52:12  来源:igfitidea点击:

Scanner class java file not found

javajava.util.scannerreadfile

提问by Abel Jojo

Scanner Class couldnt find the file I use NetBeansIDE, and the test.txt is in the folder path: D:\netbeans project works\ReadFile\src\readfile\test.txt

Scanner Class 找不到我使用 NetBeansIDE 的文件,test.txt 在文件夹路径中:D:\netbeans projectworks\ReadFile\src\readfile\test.txt

in the same folder the readfile.java exsist. the code is as below. It generates file not found.

在同一个文件夹中存在 readfile.java。代码如下。它生成未找到的文件。

package readfile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;


public class ReadFile {

    public static void main(String[] args) throws IOException , FileNotFoundException 
    {  
        Scanner scanner = new Scanner(new File("test.txt"));  

        while (scanner.hasNextLine())  
            System.out.println(scanner.nextLine());  
    }  
}

output:-

输出:-

run:
Exception in thread "main" java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at java.util.Scanner.<init>(Scanner.java:636)
    at readfile.ReadFile.main(ReadFile.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

回答by Tomasz Nurkiewicz

Add the following before creating Scannerclass:

在创建Scanner类之前添加以下内容:

System.out.println(new File("test.txt").getAbsolutePath());

It will show you where JVM expects to find the file and whether it is the folder you expect as well.

它会告诉你 JVM 期望在哪里找到文件,以及它是否也是你期望的文件夹。

Also check file permissions. But most likely it is a problem with default JVM directory.

还要检查文件权限。但很可能是默认 JVM 目录的问题。

回答by Ben

Ahhh you aren't specifying the full file path. When a file path is abbreviated (i.e. test.txt), java assumes that the file is in the same directoryas the source code that is running it. So either specify the full path, or move the file.

啊,您没有指定完整的文件路径。当文件路径被缩写(即test.txt)时,java 假定该文件运行它的源代码位于同一目录中。所以要么指定完整路径,要么移动文件。

回答by Razvan

Move it to the ReadFile directory, i.e. the root of the project

将其移动到ReadFile目录,即项目的根目录

回答by Ravi Jain

The test.txtfile should be in the folder where the file readfile.classexists.

test.txt文件应位于该文件所在的文件夹中readfile.class

回答by Sarneet Kaur

what worked for me was removing .txt extension from the file name and using . to specify current directory (example shown below).

对我有用的是从文件名中删除 .txt 扩展名并使用 . 指定当前目录(示例如下所示)。

Scanner scanner = new Scanner(new File("./test"));

回答by fgh

I am aware that this problem has been reported a long time ago, however, I have faced a similar obstacle and then the proposed solutions didn't work, thus I decided to post another answer.

我知道这个问题很久以前就被报告过,但是,我遇到了类似的障碍,然后提出的解决方案不起作用,因此我决定发布另一个答案。

Try using try... catchclause. For instance, only then my code has become compiled by NetBeans.

尝试使用try... catch子句。例如,只有这样我的代码才由 NetBeans 编译。