2012-03-26 100 views
0

一直试图让这个工作了一整天,但它总是返回错误,甚至在所有的研究,我已经做了我还是一无所知阅读目录中的jar文件

这里的方法,这就是线目前有错误返回是

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

所有我知道的是,东西需要处理作为资源,但我不确定URE我需要改变的话,我敢肯定,我做了所有的neccessary变化但它仍然没有工作

任何帮助表示赞赏

编辑:

我一直乱搞与此,这是,这似乎是相当不错的了,除了新的方法,它无法读取

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

不知道如何我想读它(现在我已经读注释掉) 任何线索任何人?

+0

你能发布你得到的错误吗? – 2012-03-26 01:19:55

+0

它只是一个空指针异常导致501行(在它的url.toURI()行) – Travs 2012-03-26 01:28:38

+0

好吧。你有没有检查url的值是否为空?你甚至可以检查用于设置url的表达式的每个部分,以查看哪个部分为空 – 2012-03-26 01:30:28

回答

2

在这里使用File.separator是错误的。资源由URL标识,而不是文件名,URL只有正斜杠。

然后,您错误地将该URL转换为文件。它可能是一个URL,它指向远程位置的JAR内部。

+0

请查看我刚刚应用到主帖子的编辑,希望这会更具信息性 – Travs 2012-03-26 03:32:15

+0

@TravisWhitmarsh'ZipEntry'有一个API。不要只是调用'toString()'并希望最好。它不读取,因为你不*阅读*。你得到输入流,但你只需关闭它。 'InputStream'也有一个API。 – EJP 2012-03-26 03:38:25

0

你可以试试下面的代码:

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

编辑:哦,我刚看到,今天你urlnull。请记住getResource("resource name")classpath寻找资源。所以你的resource name应该是/package/path/of/resourceName.extenstion

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