0

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 :)

Ralfi
  • 3
  • 2

3 Answers3

2

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);
                }
            });
Community
  • 1
  • 1
Omkar Shetkar
  • 3,488
  • 5
  • 35
  • 50
0

If you place your file

test.png

under

/src/resources/org/example

you can access it from your class

org.example.InstallFrame

using

final URL resource = InstallFrame.class.getResource("test.png");

respectively

final InputStream resourceAsStream = InstallFrame.class.getResourceAsStream("test.png");
Smutje
  • 17,733
  • 4
  • 24
  • 41
  • I need to get all files, not only one – Ralfi Aug 12 '16 at 06:05
  • 1
    @Ralfi Do you know their respective names or just an arbitrary number of files? The first is manageable by using a list of names, the second is not covered by the class-loader API for loading resources and you have to think of a naming/directory scheme for yourself (i.e. using `/tmp/installer` or any other determined directory name usable by any machine you're running your program on or make the path configurable for users of your program). – Smutje Aug 12 '16 at 06:08
  • i dont know the names sadly. I need to get all the files. Can you show me example of class loader api in my case? – Ralfi Aug 12 '16 at 06:27
  • No, as I wrote that "the second is not covered by the class-loader API for loading resources". – Smutje Aug 12 '16 at 06:36
0

First find the base path and then use in your code

 String basePath = ClassLoader.getSystemResource("").getPath();

 //move basepath to the path where img directory 
 //present in your project using ".." 
 //like basepath = basepath+"..\\..\\img"+

    Files.walk(Paths.get(testPath)).forEach(filePath -> {
        if (Files.isRegularFile(filePath)) {
            System.out.println(filePath);
        }
    });
ravthiru
  • 8,878
  • 2
  • 43
  • 52