列出和读取 JAR 文件中 java 包中的所有文件不起作用
Listing and reading all files in a java package within a JAR file not working
我有一个包,其中包含一个 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);
}
Windows 结果
[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 中它失败了,它表明 Inputstream 有 0 个字节可用,我想知道为什么。我尝试了几种解决方案,但 none 似乎给了我想要的结果。我哪里出错了?
所以经过更多的研究,上面的代码只在 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);
}
}
}
我有一个包,其中包含一个 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);
}
Windows 结果
[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 中它失败了,它表明 Inputstream 有 0 个字节可用,我想知道为什么。我尝试了几种解决方案,但 none 似乎给了我想要的结果。我哪里出错了?
所以经过更多的研究,上面的代码只在 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);
}
}
}