一整天都在尝试让它工作,但它总是返回错误,即使在我完成了所有研究之后,我仍然一无所知
这是方法,当前返回错误的行是
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/