我的java 项目中有些图片,在调试环境下调用没有问题。
现在打包成jar包来运行,一般而言用getResource来访问,但访问单个图片是没有问题的。我现在要遍历某个目录怎么做?
例如:应用程序打包成
a.jar
内容包括
Main.class
K.class
images/a.png
images/b.png
images/c.gif
images/d.txt我现在要遍历images目录,取出所有的png文件,怎么办?
现在打包成jar包来运行,一般而言用getResource来访问,但访问单个图片是没有问题的。我现在要遍历某个目录怎么做?
例如:应用程序打包成
a.jar
内容包括
Main.class
K.class
images/a.png
images/b.png
images/c.gif
images/d.txt我现在要遍历images目录,取出所有的png文件,怎么办?
楼主【cxz7531】截止到2008-07-22 11:00:43的历史汇总数据(不包括此帖):
发帖的总数量:29 发帖的总分数:2560 每贴平均分数:88
回帖的总数量:370 得分贴总数量:124 回帖的得分率:33%
结贴的总数量:27 结贴的总分数:2440
无满意结贴数:0 无满意结贴分:0
未结的帖子数:2 未结的总分数:120
结贴的百分比:93.10 % 结分的百分比:95.31 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
值得尊敬
http://www.fh888.com/showfile.html?articleid=C31E462FF40045D0BF88926F9FE50C38&projectid=5&username=yhn
一般项目中的按钮图片都是这样访问的,但问题是我现在要遍历images下的内容,而且过滤png文件就不知道怎么搞了
java.util.jar.JarFile file = new JarFile("f:\\dwr.jar");
Enumeration<JarEntry> entrys = file.entries();
while(entrys.hasMoreElements()){
JarEntry jar = entrys.nextElement();
System.out.println(jar.getName());
}
file.close();
}
你让他访问自己不就行了。呵呵! 别钻牛角尖啊!比如你运行的jar就叫 dwr.jar ,你的程序运行后,打开自己就行了!
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;/**
* JarFileRes
* @author SageZk
*/
public class JarFileRes { public static void main(String[] args) {
String ext = "jpg"; //要过滤的扩展名
String jar = JarFileRes.class.getProtectionDomain().getCodeSource().getLocation().getFile();
ext = ext.toLowerCase();
List<String> list = new LinkedList<String>();
JarFile jf = null;
try {
jf = new JarFile(jar);
Enumeration<JarEntry> es = jf.entries();
while (es.hasMoreElements()) {
String resname = es.nextElement().getName();
if (resname.toLowerCase().lastIndexOf(ext) != -1) list.add(resname);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (jf != null) {
try {
jf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
for (String s : list) {
URL url = JarFileRes.class.getResource("/".concat(s));
System.out.println(url.toString());
}
}}
public class TestJarURLConnection { /**
* @param args
* @throws IOException
* @throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException, IOException {
// TODO Auto-generated method stub
URL url = TestJarURLConnection.class.getClassLoader().getResource("com/runway/booster/misc/image");
JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();
System.out.println(jarURLConnection.getJarFile().getName());//打印jar文件路径
JarFile jarFile = jarURLConnection.getJarFile();
Enumeration<JarEntry> entrys = jarFile.entries();
while(entrys.hasMoreElements()){
JarEntry imagEntry = entrys.nextElement();
if (imagEntry.getName().matches(".+\\/image\\/[^\\/]+\\.png")) {
System.out.println(imagEntry.getName());
InputStream jis = jarFile.getInputStream(imagEntry);
FileOutputStream fos = new FileOutputStream("d:"+imagEntry.getName().substring(imagEntry.getName().lastIndexOf("/")));
byte[] buff = new byte[4096];
int readCount = 0;
while ((readCount = jis.read(buff)) != -1) {
fos.write(buff, 0, readCount);//将image目录内的png文件保存到d盘
}
fos.close();
}
}
jarFile.close();
}}