gpt4 book ai didi

java - 如何计算 JAR 中文件夹中的文件数?

转载 作者:搜寻专家 更新时间:2023-11-01 02:47:31 25 4
gpt4 key购买 nike

我花了一些时间试图找到一种方法来计算 JAR 中文件夹中的文件数。我将几个服务于不同目的的代码示例放在一起来完成这项工作。当我通过 Eclipse 运行代码但在导出到 JAR 后它会失败并返回 0 时它算得上很好。在这种情况下,我使用的文件夹路径只是“rules/”。我将不胜感激任何建议或 sample 。谢谢。

public static int countFiles(String folderPath) throws IOException { //Counts the number of files in a specified folder
ClassLoader loader = ToolSet.class.getClassLoader();
InputStream is = loader.getResourceAsStream(folderPath);
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}

编辑:以下内容与我原来的问题并不完全匹配,但多亏了 MadProgrammer,我才得以减少代码,甚至无需对文件进行计数。代码 blow 搜索我的 JAR 中的每个文件,寻找以“.rules”结尾的文件,打开文件,在文件中搜索匹配“searchBox.getText()”的字符串,附加结果,然后继续下一个“.rules”文件。

    StringBuilder results = new StringBuilder();
int count = 0;
JarFile jf = null;
try {
String path = ToolSet.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
jf = new JarFile(new File(decodedPath));
Enumeration<JarEntry> entries = jf.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().endsWith(".rules")) {
String name = entry.getName();
InputStream in = ToolSet.class.getResourceAsStream(name);
InputStreamReader isr = new InputStreamReader(in);
BufferedReader bf = new BufferedReader(isr);
String line;
while ((line = bf.readLine()) != null) {
String lowerText = line.toLowerCase();
if(lowerText.indexOf(searchBox.getText().toLowerCase()) > 0) {
results.append(line + "\n");
count++;
}
}
bf.close();
}
}
} catch (IOException ex) {
try {
jf.close();
} catch (Exception e2) {
}
}
if(count>0) {
logBox.setText(results.toString());
} else {
logBox.setText("No matches could be found");
}

最佳答案

Jar 文件本质上是带有 list 的 Zip 文件。

Jar/Zip 文件实际上没有像磁盘那样的目录概念。他们只是有一个有名字的条目列表。这些名称可能包含某种路径分隔符,并且某些条目实际上可能被标记为目录(并且往往没有任何字节与之关联,仅充当标记)

如果你想找到给定路径中的所有资源,你将不得不打开 Jar 文件并自己检查它的条目,例如......

JarFile jf = null;
try {
String path = "resources";
jf = new JarFile(new File("dist/ResourceFolderCounter.jar"));
Enumeration<JarEntry> entries = jf.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
String name = entry.getName();
name = name.replace(path + "/", "");
if (!name.contains("/")) {
System.out.println(name);
}
}
}
} catch (IOException ex) {
try {
jf.close();
} catch (Exception e) {
}
}

现在,这需要您知道要使用的 Jar 文件的名称,这可能会有问题,因为您可能希望列出来自许多不同 Jar 的资源...

更好的解决方案是在构建时生成某种“资源查找”文件,其中包含您可能需要的所有资源名称,甚至可能键入特定名称...

这样你就可以简单的使用...

BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsInputStream("/resources/MasterResourceList.txt")));
String name = null;
while ((name = br.readLine()) != null) {
URL url = getClass().getResource(name);
}
} finally {
try {
br.close();
} catch (Exception exp) {
}
}

例如……

您甚至可以使用资源数量为文件播种 ;)

关于java - 如何计算 JAR 中文件夹中的文件数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18758105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com