使用InputStreamReader读取JAR中的目录 [英] Read directory inside JAR with InputStreamReader

查看:251
本文介绍了使用InputStreamReader读取JAR中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这个问题已被问过我相信的一百万次,而且我已经读了几个小时,尝试了一些人提供的几个选项,但没有一个适合我。

So, this question has been asked a million times i believed and I've been reading them for a couple of hours and trying several options given by some people but none of them work for me.

我想列出应用程序JAR中目录中的所有文件,因此在IDE中这可行:

I want to list all the files inside a directory inside the application's JAR, so in IDE this works:

File f = new File(this.getClass().getResource("/resources/").getPath());

for(String s : f.list){
   System.out.println(s);
}

这给了我目录中的所有文件。

That gives me all the files inside the directory.

现在,我也尝试了这个:

Now, i've tried this also:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("resources/");
    InputStreamReader inReader = new InputStreamReader(in);
    Scanner scan = new Scanner(inReader);

    while (scan.hasNext()) {
        String s = scan.next();
        System.out.println("read: " + s);
    }

    System.out.println("END OF LINE");

从IDE中打印出目录中的所有文件。外部IDE打印:END OF LINE。

And from IDE it prints ALL the files in the directory. Outside IDE prints: "END OF LINE".

现在,我也可以在Jar中找到一个条目:

Now, I can find an entry inside a Jar with this too:

        String s = new File(this.getClass().getResource("").getPath()).getParent().replaceAll("(!|file:\\\\)", "");
        JarFile jar = new JarFile(s);

            JarEntry entry = jar.getJarEntry("resources");

        if (entry != null){
            System.out.println("EXISTS");
            System.out.println(entry.getSize());
        }

这是我必须对该字符串做的一些可怕的编码。

That's some horrible coding i had to do to that String.

无论如何......我无法获取Jar内resources目录内的资源列表......我该怎么办?

Anyway... I can't get the list of resources inside the "resources" directory within the Jar... How can I do this???

推荐答案

如果没有先枚举Jar文件的内容,就无法简单地获取内部资源的过滤列表。

There's no way to simply get a filtered list of internal resources without first enumerating over the contents of the Jar file.

幸运的是,这实际上并不那么难(幸运的是,你已经完成了大部分的努力工作)。

Luckily, that's actually not that hard (and luckily for me you've done most of the hardwork).

基本上,一旦你引用了 JarFile ,你就可以简单地询问它的'条目并迭代该列表。

Basically, once you have a reference to the JarFile, you simple need to ask for its' entries and iterate over that list.

通过检查所需匹配的 JarEntry 名称(即资源),你可以过滤你想要的元素...

By checking the JarEntry name for the required match (ie resources), you can filter the elements you want...

例如......

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class ReadMyResources {

    public static void main(String[] args) {
        new ReadMyResources();
    }

    public ReadMyResources() {
        JarFile jf = null;
        try {            
            String s = new File(this.getClass().getResource("").getPath()).getParent().replaceAll("(!|file:\\\\)", "");
            jf = new JarFile(s);

            Enumeration<JarEntry> entries = jf.entries();
            while (entries.hasMoreElements()) {
                JarEntry je = entries.nextElement();
                if (je.getName().startsWith("resources")) {
                    System.out.println(je.getName());
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                jf.close();
            } catch (Exception e) {
            }
        }
    }

}

警告

这类问题实际上有点问题。而不是尝试在运行时读取Jar的内容,最好生成某种包含可用资源列表的文本文件。

This type of question actually gets ask a bit. Rather then trying to read the contents of the Jar at runtime, it would be better to produce some kind of text file which contained a list of the available resources.

这可能在创建Jar文件之前,由您的构建过程动态生成。这将是一个更简单的解决方案,然后读取此文件(例如,通过 getClass()。getResource()),然后在文本文件中查找每个资源列表......恕我直言

This could be produced by your build process dynamically before the Jar file is created. It would be a much simpler solution to then read this file in (via getClass().getResource(), for example) and then look up each resource list in the text file...IMHO

这篇关于使用InputStreamReader读取JAR中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆