简体   繁体   中英

FileNotFoundException. i dont understand

i can seem to understand why my code isn't compiling. Everytime I run it, I get a FILENOTFOUNDException. Any help would be much appreciated. :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.
+ 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.

To check what is that desired input files directory simply use getAbsolutePath() on that file.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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