How to read files in inputstream folder

I have a Jar file that I created using a third-party library. When I packed the jar file, I include several XML files in it in a folder called data strong>

data - file1.xml - file2.xml - file3.xml 

Now I wanted to read the folder inside the jar file and according to the documentation of the third-party library I could get the class loader and read the folder as input stream like this.

 ClassLoader clsLoader = myService.getClassLoader(); InputStream accountsStream = clsLoader.getResourceAsStream("data"); 

The question is, how can I list all the files from the input stream and parse it one by one?

thanks

EDIT Added information:

 How do I access resources that I put into my service or module archive file? 

http://axis.apache.org/axis2/java/core/faq.html#b1

Sorry, the question was supposed to be specific to Apache Axis, but I'm a little confused if this is also a specific Java question.

After entering the input stream into a folder using the class loader, how can I list all the files in this folder and read them one at a time?

The steps in my code will be hidden.

  • Get input stream to folder
  • List all files from this input stream
  • Read it one by one.
+4
source share
2 answers

(Sorry - ignore my answer - better here :)

How to list files inside a jar file?

This is a pretty fragile solution, but you can just read your own jar file:

 File file = new File(System.getProperty("user.dir") + "/jarname.jar"); JarFile jfile = new JarFile(file); Enumeration e = jfile.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry)e.nextElement(); String path = entry.getName(); if(path.startsWith("/path/within/jarfile/") && path.endsWith(".xml")) { MyClass.loadResourceAsStream(path); } } 

What makes it fragile is that it depends on your jarfile having a specific name, and not inside another jarfile, etc. I am sure there is a more elegant way ...

+2
source

When I packed the jar file, I include several XML files in it in a folder called data p>

  • Also include a list called (e.g.) data/listOfXML.txt at the same time.
  • Get the list as a resource.
  • Read the list for XML file names.
+2
source

Source: https://habr.com/ru/post/1390154/


All Articles