简体   繁体   English

读取jar文件中的目录

[英]Read a directory in a jar file

Been trying to get this working all day but it always returns in errors and even after all the research i've done i'm still clueless 一直试图让这一整天工作,但它总是返回错误,即使经过我所做的所有研究,我仍然无能为力

here's the method, the line that's currently returning with an error is 这是方法,当前返回错误的行是

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

i'm unsure what it is that i have to do to make it work 我不确定我必须做些什么才能让它发挥作用

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();
    }
}

all i'm aware of is that the stuff needs to be treated as resources, however i'm unsure what i need to change to do so, i'm sure i've made all the neccessary changes however it's still not working 我所知道的是,这些东西需要被视为资源,但是我不确定我需要改变什么,我确定我做了所有必要的改变但是它仍然没有工作

any help appreciated 任何帮助赞赏

EDIT: 编辑:

i've been messing around with this and this is the new method that seems to be working pretty well except it can't read 我一直在搞乱这个,这是一个似乎工作得很好的新方法,除了它无法阅读

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();
    }
}

not sure how i'm suppose to read it (right now i have the reading commented out) any clues anyone? 不知道我怎么想读它(现在我的阅读评论了)任何线索?

You are wrong to use File.separator here. 你在这里使用File.separator是错误的。 A resource is identified by a URL, not a filename, and URLs only have forward slashes. 资源由URL标识,而不是文件名,URL只有正斜杠。

You are then wrong to turn that URL into a File. 然后将该URL转换为文件是错误的。 It may be a URL that points inside a JAR at a remote location. 它可能是指向远程位置的JAR内部的URL。

You can try following code: 您可以尝试以下代码:

URL url = getClass().getResource(/* resource name */);
final URL resolve = org.eclipse.core.runtime.FileLocator.resolve(url);
File file = new File( resolve.getPath());

EDIT: Oh, I just saw, you getting your url as null . 编辑:哦,我刚刚看到,你的urlnull Remember that getResource("resource name") looks for the resource in classpath . 请记住, getResource("resource name")classpath查找资源。 So your resource name should be /package/path/of/resourceName.extenstion 所以你的resource name应该是/package/path/of/resourceName.extenstion

eg /com/kjain/widget/identifier/views/view.jpg 例如/com/kjain/widget/identifier/views/view.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM