我有一个包,其中包含 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] ->
b2b
b2c
bal
c2b
olp
olq
reg
rev
stat
Linux 结果
[Path] -> jar:file:/path/to/application/lib/my-jarfile.jar!/this/is/my/package/containing/data/
[Files] -> []
[available bytes] -> 0
在 Windows 中它工作得很好,但在 Linux 中却失败了,它显示输入流有 0 个可用字节,我想知道为什么。我尝试了几种解决方案,但似乎都没有给我我想要的结果。我可能出什么问题或哪里出了问题?
墨色风雨
相关分类