Ralfi Ralfi - 3 months ago 20
Java Question

How to get files from a project folder

The folder is

/src/img
. I need to get all file names(
png
files) from
img
folder and all subfolders.I dont know the full path to the folder so i need only to focus the project resourse. I need them in array. Using Java8 and Eclipse Mars.
So far this works:

try {


Files.walk(Paths.get("/home/fixxxer/NetBeansProjects/Installer/src/img/")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
System.out.println(filePath);
}
});
} catch (IOException ex) {
Logger.getLogger(InstallFrame.class.getName()).log(Level.SEVERE, null, ex);
}


The problem is that the folder need to be called like this:
/img
cause it is inside the project
Thanks in advance :)

Answer

You can use relative paths. Here, I am assuming that Installer is your project name. Otherwise, you can edit the relative path according to your project setup.

Files.walk(Paths.get(".//src//img//")).forEach(filePath -> {
                if (Files.isRegularFile(filePath)) {
                    System.out.println(filePath);
                }
            });
Comments