0

I have a question which is not answered by this question (I believe). I have two separate maven projects and in one of them I want to go through all files (they happen to be .txt files) inside a certain directory. This directory lies inside the resource folder of my first maven project. Then I want to export my first maven project and import it in to SceneBuilder as a .jar and still have the importing of .txt files working. When this is done I want to use this .jar as a custom component inside my second maven project.

For some clarification here is the exact setup in my case. Here is the directory of my first maven project:

enter image description here

From a .java file called GradientPicker inside of com.thefractory.customcomponents I want to loop through the gradients folder seen above and want to read all .txt files in it. There will be a different number of files each time so an enhanced for-loop would be nice. This part I have actually managed to do but only when GradientPicker is run from Eclipse. I want this to work when my project is run as a .jar as well.

How do I loop through a directory and read all files from within a .jar?

Ivar Eriksson
  • 863
  • 1
  • 15
  • 30
  • There are no directories inside Jars. There are resources and resource paths (which might look like directories, but aren't). ***"How do I loop through a directory and read all files from within a .jar?"*** Since you are apparently building the Jar, prepare and include a list of the resources of interest. – Andrew Thompson Jun 30 '18 at 15:13
  • @Andrew Thompson Ooh... Can I ask a followup question then. 1: how do you suggest that I include this list? Hard code it? 2: What would you say is the best way to let a user save files in the jar and then read these files next time the jar is opened? – Ivar Eriksson Jun 30 '18 at 15:20
  • 1
    *"..the best way to let a user save files in the jar and then read these files next time the jar is opened?"* See [How can an app use files inside the JAR for read and write?](https://stackoverflow.com/q/5052311/418556).. – Andrew Thompson Jun 30 '18 at 15:22

1 Answers1

0
    JarFile jf = new JarFile(new File("jar path"));
    jf.stream().filter(entry -> !entry.isDirectory()
            && entry.getName().startsWith("com/thefractory/customcomponents/gradients/")
            && entry.getName().endsWith(".txt"))
            .forEach(jarEntry -> {
                System.out.println("jarEntry.getName() = " + jarEntry.getName());
            });
gagan singh
  • 1,591
  • 1
  • 7
  • 12