简体   繁体   中英

How to Read files in an folder inputstream

I have a Jar file that I have created using 3rd party library. When I packaged the jar file, I am including several xml files inside it in a folder named data

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

Now, I wanted to read the folder inside the jar file and as per the documentation of the 3rd party library I could get the classloader and read the folder as inputstream like this.

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

Question is, how can I list all the files from the inputstream and parse it one by one?

Thanks

EDIT Added Info:

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 should have been specific to Apache Axis but I am confused a little bit if it is a Java specific question also.

After getting an inputstream to a folder using the classloader, how do I list all the files into that folder and read it one by one?

The steps in my code would inlcude.

  1. Get an inputstream into the folder
  2. List all files from that inputstream
  3. Read it one by one

(Sorry - ignore my answer - here's a better one:)

How do I list the files inside a JAR file?

This is a rather fragile solution, but you could 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 particular name, not being inside another jarfile, and so on. I'm sure there's a more elegant way...

When I packaged the jar file, I am including several xml files inside it in a folder named data

  1. Also include a list called (eg) data/listOfXML.txt at the same time.
  2. Obtain the list as a resource.
  3. Read the list to get the names of the XML files

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