亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

列出并讀取 JAR 文件中 java 包中的所有文件不起作用

列出并讀取 JAR 文件中 java 包中的所有文件不起作用

MMMHUHU 2024-01-05 19:58:42
我有一個包,其中包含 jar 文件中的文件列表。這個想法是找到包中的所有文件并讀取每個文件的內容。private static final String DATA_DIRECTORY = "this/is/my/package/containing/data/";try {            URL url = this.getClass().getClassLoader().getResource(DATA_DIRECTORY);            if (url.toExternalForm().startsWith("jar:")) {                try (BufferedReader in = new BufferedReader(                        new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(DATA_DIRECTORY)))) {                    String inputLine;                    while ((inputLine = in.readLine()) != null) {                        System.out.println(inputLine);                    }                }            } else {                try (BufferedReader in = new BufferedReader(                        new InputStreamReader(url.openStream()))) {                    String inputLine;                    while ((inputLine = in.readLine()) != null) {                        System.out.println(inputLine);                    }                }            }        } catch (Exception e) {            System.out.println(e);        }窗口結果[Path] -> file:/D:/path/to/application/lib/my-jarfile.jar!/this/is/my/package/containing/data/[Files] ->b2bb2cbalc2bolpolqregrevstatLinux 結果[Path] -> jar:file:/path/to/application/lib/my-jarfile.jar!/this/is/my/package/containing/data/[Files] -> [][available bytes] -> 0在 Windows 中它工作得很好,但在 Linux 中卻失敗了,它顯示輸入流有 0 個可用字節,我想知道為什么。我嘗試了幾種解決方案,但似乎都沒有給我我想要的結果。我可能出什么問題或哪里出了問題?
查看完整描述

1 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

因此,經過更多研究后,上述代碼只能在 Windows 的 IDE 中運行,但對于 jar 文件則失敗,因此我在 Linux 和 Windows 中得到的結果。


以下是我正在使用的工作解決方案。它可以以更簡潔的方式重寫,但由于時間原因,我保留了原樣。它適用于類路徑和 Jar 文件。


String directory = "/this/is/my/package/containing/data/";

請注意,包名稱目錄中需要開頭斜杠。


String path = ClassLoader.class.getResource(directory).toExternalForm();

        if (path.startsWith("jar")) {

            try {

                URL url = new URL(path);

                System.out.println("JAR URL Name : " + url.toString());

                JarURLConnection connection = (JarURLConnection) url.openConnection();

                String jarFileName = connection.getJarFile().getName();

                System.out.println("JAR File Name : " + jarFileName);

                try (JarInputStream jarFile = new JarInputStream(new FileInputStream(jarFileName));) {

                    String regex = directory.substring(1);

                    do {

                        try {

                            JarEntry jarEntry = jarFile.getNextJarEntry();

                            if (jarEntry != null) {

                                String entryName = jarEntry.getName();

                                if (!regex.equals(entryName) && entryName.contains(regex)) {

                                    System.out.println("JAR Entry Name : " + entryName);

                                    try (InputStream in = YourClassName.class.getClassLoader().getResourceAsStream(entryName);

                                            Scanner scanner = new Scanner(in);) {

                                    //Do something in here                                       

                                    } catch (Exception ex) {

                                        System.out.println(ex);

                                    }

                                }

                            } else {

                                break;

                            }

                        } catch (IOException ex) {

                            System.out.println("JAR InputStream Error : " + jarFileName + " - > " + ex);

                            break;

                        }

                    } while (true);

                }

            } catch (Exception e) {

                System.out.println("JAR URL Connection Error : " + e);

            }

        } else {

            try {

                URL url = new URL(path);

                try (BufferedReader in = new BufferedReader(

                        new InputStreamReader(url.openStream()))) {

                    String fileName;

                    while ((fileName = in.readLine()) != null) {

                        String filePath = directory + fileName;

                        try (InputStream is = ClassLoader.class.getResourceAsStream(filePath);

                                Scanner scanner = new Scanner(is);) {

                           //Do something here

                        } catch (Exception ex) {

                            System.out.println(ex);

                        }

                    }

                }

            } catch (Exception e) {

                System.out.println("Classpath URL Connection Error : " + e);

            }

        }

    }


查看完整回答
反對 回復 2024-01-05
  • 1 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號