java - 读取jar文件中的目录

标签 java arrays resources jar directory

一整天都在尝试让它工作,但它总是返回错误,即使在我完成了所有研究之后,我仍然一无所知

这是方法,当前返回错误的行是

File file = new File( url.toURI() );

我不确定我必须做什么才能让它发挥作用

public void preloadModelsTwo() {
    String slash = System.getProperty("file.separator");
    try {
        URL url = getClass().getResource("."+ slash +"Patches" + slash+"Models"+slash);
        File file = new File( url.toURI() );
        File[] fileArray = file.listFiles();
        for(int y = 0; y < fileArray.length; y++) {
            String s = fileArray[y].getName();
            if (s != "") {
                byte[] buffer = readFile("."+ slash +"Patches" + slash+"Models"+slash+""+s);
                Model.method460(buffer, Integer.parseInt(getFileNameWithoutExtension(s)));
                //System.out.println("Read model: " + s);
            }
        }
    } catch (Exception E) {
        System.out.println("error with secondary model screening");
        E.printStackTrace();
    }
}

我所知道的是这些东西需要被视为资源,但是我不确定我需要改变什么才能做到这一点,我确信我已经做了所有必要的改变,但它仍然没有工作

感谢任何帮助

编辑:

我一直在搞这个,这是一种新方法,除了无法读取之外,似乎工作得很好

public String removeNonDigits(String text) {
    int length = text.length();
    StringBuffer buffer = new StringBuffer(length);
    for(int i = 0; i < length; i++) {
        char ch = text.charAt(i);
        if (Character.isDigit(ch)) {
            buffer.append(ch);
        }
    }
    return buffer.toString();
}

public JarEntry entry;
public void preloadModelsTwo() {
    String slash = System.getProperty("file.separator");
    try {
JarFile jarFile = new JarFile("SilabGarza.jar");
for(Enumeration em = jarFile.entries(); em.hasMoreElements();) {  
    String s= em.nextElement().toString();  
    if(s.contains(".dat")){
        entry = jarFile.getJarEntry(s);
        InputStream input = jarFile.getInputStream(entry);
        System.out.println("input = "+input);
        //byte[] buffer = readFile(s);
        String s2 = removeNonDigits(s);
        //Model.method460(buffer, Integer.parseInt(s2));
        System.out.println("Found: "+s);
        System.out.println("formd: "+s2);

        input.close();
    }  
}
jarFile.close();

    } catch (Exception E) {
        System.out.println("error with secondary model screening");
        E.printStackTrace();
    }
}

不知道我应该如何阅读它(现在我已经注释掉了阅读内容) 有人有任何线索吗?

最佳答案

您在这里使用 File.separator 是错误的。资源由 URL(而不是文件名)标识,并且 URL 仅包含正斜杠。

您将该 URL 转换为文件的做法是错误的。它可能是指向远程位置的 JAR 内部的 URL。

关于java - 读取jar文件中的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9865489/

相关文章:

java - 无法从另一个类打印到 JTextArea

java - 2x ArrayList 的一个循环

JavaFx 按钮在 E4 ToolControl 上呈现时添加省略号

Android:在没有 Activity 或 Context 引用的情况下访问资源

android - 获取Android文件资源的大小?

f# - 如何在F#项目中管理资源?

java - 扫描仪错误 未找到符号

c - 结构中空数组的 strlen() 不为 0

c - 反转字符串变量,或反转打印字符数组

javascript - 为什么不能用索引访问这个数组?