简体   繁体   English

文件未找到异常。 我不明白

[英]FileNotFoundException. i dont understand

i can seem to understand why my code isn't compiling.我似乎可以理解为什么我的代码没有编译。 Everytime I run it, I get a FILENOTFOUNDException.每次运行它时,都会收到 FILENOTFOUNDException。 Any help would be much appreciated.任何帮助将非常感激。 :D :D

public static void main(String args[]) throws IOException 
    {

        Scanner diskScanner = 
                new Scanner(new File("EmployeeInfo.txt"));

        for(int empNum = 1; empNum<=3; empNum++)
        {
            payOneEmployee(diskScanner);
        }

    }
    static void payOneEmployee(Scanner aScanner)
    {
        Employee anEmployee = new Employee();

        anEmployee.setName(aScanner.nextLine());
        anEmployee.setJobTitle(aScanner.nextLine());
        anEmployee.cutCheck(aScanner.nextDouble());
        aScanner.nextLine();
    }

Basically the exception message means the filename you specified is not an existing file in executions directory.基本上异常消息意味着您指定的文件名不是执行目录中的现有文件。

EDIT [copied from my comment]编辑[从我的评论中复制]
That file should be located where compilation is done, if you are using eclipse or intellij it should be at your projects root directory.该文件应位于完成编译的位置,如果您使用的是 eclipse 或 intellij,则它应位于您的项目根目录中。
+ Because you are passing in a relative path and not an absolute one to the file, java is recognizing it as a relative to execution directory which is located where followin code points to. + 因为您传递的是相对路径而不是文件的绝对路径,所以java 将其识别为相对于执行目录的相对路径,该目录位于后续代码指向的位置。

To check what is that desired input files directory simply use getAbsolutePath() on that file.要检查所需的输入文件目录是什么,只需对该文件使用getAbsolutePath()即可。
For instance:例如:

File input = new File("EmployeeInfo.txt");
System.out.println("Move .txt to dir:" + input.getAbsolutePath());
Scanner diskScanner = new Scanner(input);

Then move the source .txt file to that location然后将源.txt文件移动到该位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM