public class JarEntryTest {
public static void main(String[] args) throws IOException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
JarFile jarFile = new JarFile("d:/quartz-1.6.5.jar");
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
InputStream in = null;
List<String> lines = null;
int c = 0;
URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
method.invoke(classLoader, new Object[]{new URL("file:/d:/quartz-1.6.5.jar")});
Enumeration<JarEntry> entrys = jarFile.entries();
while (entrys.hasMoreElements()) {
JarEntry entry = entrys.nextElement();
String name = entry.getName();
if(name.endsWith(".properties")) {
try {
in = classLoader.getResourceAsStream(name);
lines = IOUtils.readLines(in);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
IOUtils.closeQuietly(in);
}
for (String line : lines) {
System.out.println(name + " " + line);
}
}
}
}
}
了解这点后,对于读取国际化资源文件有所帮助
这里需要温习一下getdeclaredmethod getmethod之间的区别