为什么在File的父文件夹上工作会返回null或NoSuchFileException?

时间:2017-08-12 23:11:20

标签: java nio nio2

我想获取文件的父文件夹,列出其所有文件并将其作为Vector返回。不幸的是,当我尝试创建目录流时,我收到NoSuchFileException。我有点陷入黑暗,因为上面的几行我通过file.getParent()访问了该文件夹,甚至列出了它的路径。我可以从file创建一个图像并获得它的高度,以确保我访问正确的目录。有人能告诉我发生了什么吗?

private Vector<File> getFiles() {
        File file = new File("sample/emot1.jpg");
        System.out.println("File path: " + file.getPath() + " File name: " + file.toString());

        Image image = new Image(file.getPath());
        System.out.println("Loaded image's height: " + image.getHeight());

        File fileParent = file.getParentFile();
        System.out.println("Parent file path: " + fileParent.getPath() + " Parent file name: " + fileParent.toString());

        Path dir = Paths.get(fileParent.getPath());
        Vector<File> files = new Vector<>();

        try {
            DirectoryStream<Path> pathStream = Files.newDirectoryStream(dir);
            for (Path path : pathStream) {
                files.add(path.toFile());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return files;
    }

我尝试通过file.listFiles()方法列出文件夹的文件,但它返回null,将getPath()更改为getAbsolutePath(),但无论如何都抛出了异常。 我使用Ubuntu并且我读到它可能是一个权限发布但我在访问这些文件时没有问题 - 我甚至可以通过我的应用程序列出该文件夹的每个文件的内容,但仅限于使用直接路径时。

异常堆栈跟踪:

java.nio.file.NoSuchFileException: sample
    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
    at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:518)
    at java.nio.file.Files.newDirectoryStream(Files.java:457)
    at sample.Controller.getFiles(Controller.java:36)
    at sample.Controller.initialize(Controller.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)

0 个答案:

没有答案