문제

I've created a simple Java application that tries to read in a file, and complete an action on the file.

Problem: Upon requesting the file with the absolute path I get an FileNotFoundException.

public static void main(String... args) throws Exception {

    String path = "/Users/kentandersen/Downloads/greendaoprotobuf-master/test";
    File files = new File(path);

    System.out.println(files.getAbsoluteFile());
    System.out.print(files.canRead() + "\n");

    //Error occurs here.
    String[] fileNames = new Scanner(files, "UTF-8").useDelimiter("\\A").next().split("\n");
}

Below are the logs.

/Users/kentandersen/Downloads/greendaoprotobuf-master/test
true
Exception in thread "main" java.io.FileNotFoundException: /Users/kentandersen/Downloads/greendaoprotobuf-master/test (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.util.Scanner.<init>(Scanner.java:654)

Process finished with exit code 1

The logs clearly state that I can read those files, and then Scanner runs and says it doesn't exist.

I can copy the absolute path in the logs and CD to that location, and it takes me there.

I'm using Intellij 13.

도움이 되었습니까?

해결책

You are using Scanner on a directory. You should use it on a file.

/Users/kentandersen/Downloads/greendaoprotobuf-master/test 

is a directory.

Try something like

files.listFiles()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top