0

I want to get file's parent folder, list all of its files and return them as a Vector. Unfortunately, when I try to create a directory stream, I receive the NoSuchFileException. I'm a bit in a dark, because a few lines above I accessed that folder through file.getParent() and even listed it's path. I can create an Image from the file and get its height so for sure I access the correct directory. Can someone tell me what's happening?

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;
    }

I tried to list folder's file via file.listFiles() method but it returned null, changed getPath() to getAbsolutePath() but the exception was thrown anyway. I use Ubuntu and I read that it might be a permissions issiue but I have no problems in accessing these files - I can even list a content of every single file of that folder via my application, but only when using a direct path.

Exception stack trace:

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)
  • Are you sure `sample` actually exists in the correct place? I just tried your code and it runs without any issues. The file itself doesn't even need to exist. – Oleg Aug 12 '17 at 23:22
  • Note that you don't actually access anything before this line: `DirectoryStream pathStream = Files.newDirectoryStream(dir);` – Oleg Aug 12 '17 at 23:25
  • Is it possible for you to update the question with exception stack trace? – nagendra547 Aug 13 '17 at 08:31
  • ``sample`` is a folder inside of my ``src`` folder, inside of which are all project files. It exists for sure - look at the 'image' line I added. I can load an image via ``file`` and print its height. How I see it is that if I can access the image, I should be able to access its parent folder what, unfortunatelly, is not possible. Funny thing is that you say it runs without any problems. – Przemysław Długoszewski-Tamoń Aug 13 '17 at 08:34

0 Answers0