簡體   English   中英

使用InputStreamReader讀取JAR中的目錄

[英]Read directory inside JAR with InputStreamReader

所以,這個問題已被問過我相信的一百萬次,而且我已經讀了幾個小時,嘗試了一些人給出的幾個選項,但沒有一個適合我。

我想列出應用程序JAR中目錄中的所有文件,因此在IDE中這有效:

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

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

這給了我目錄中的所有文件。

現在,我也試過這個:

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”。

現在,我也可以在Jar中找到一個條目:

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

這是我必須對那個String做的一些可怕的編碼。

無論如何......我無法獲得Jar內“resources”目錄內的資源列表......我該怎么做?

在沒有首先枚舉Jar文件的內容的情況下,無法簡單地獲取內部資源的過濾列表。

幸運的是,這實際上並不那么難(幸運的是,你已經完成了大部分的努力工作)。

基本上,一旦你有了對JarFile的引用,你就需要簡單地詢問它的' entries並迭代該列表。

通過檢查所需匹配的JarEntry名稱(即resources ),您可以過濾所需的元素...

例如...

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的內容,最好生成某種包含可用資源列表的文本文件。

這可以由您的構建過程在創建Jar文件之前動態生成。 這將是一個更簡單的解決方案然后讀取此文件(例如通過getClass().getResource() )然后查找文本文件中的每個資源列表...恕我直言

對於Spring Framework用戶,請查看PathMatchingResourcePatternResolver以執行以下操作:

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath:path/to/resource/*.*");

for (Resource resource : resources) {
    InputStream inStream = resource.getInputStream();
    // Do something with the input stream
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM