如何读取文件夹输入流中的文件

发布于 2024-12-26 00:47:03 字数 907 浏览 0 评论 0原文

我有一个使用 3rd 方库创建的 Jar 文件。 当我打包 jar 文件时,我将几个 xml 文件包含在名为 data 的文件夹中。

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

现在,我想读取 jar 文件内的文件夹并根据第 3 方库的文档 我可以获取类加载器并将文件夹作为输入流读取,如下所示。

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

问题是,如何列出输入流中的所有文件并一一解析?

谢谢

编辑 添加信息:

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

抱歉,这个问题应该是特定于 Apache Axis 的,但如果它也是一个 Java 特定问题,我会有点困惑。

使用类加载器将输入流获取到文件夹后,如何列出该文件夹中的所有文件并一一读取它?

我的代码中的步骤将包括。

  1. 将输入流放入文件夹中
  2. 列出该输入流中的所有文件
  3. 一一读取

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天暗了我发光 2025-01-02 00:47:03

(抱歉 - 忽略我的答案 - 这是一个更好的答案:)

如何列出 JAR 文件中的文件?

这是一个相当脆弱的解决方案,但是您可以只读取您自己的 jar 文件:

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

使它脆弱的原因是它取决于您的jarfile 具有特定的名称、不在另一个 jarfile 内等等。我确信有一种更优雅的方式......

(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...

跨年 2025-01-02 00:47:03

当我打包 jar 文件时,我在名为 data 的文件夹中包含了几个 xml 文件

  1. 同时还包含一个名为(例如)data/listOfXML.txt 的列表。
  2. 获取列表作为资源。
  3. 读取列表以获取 XML 文件的名称

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 (e.g.) 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文