简体   繁体   中英

Java - Import Read Directory From Jar

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:

在此处输入图片说明

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM