JarFile jarFile = new JarFile("a.jar"); JarEntry entry = jarFile.getJarEntry("source/a.txt");
JarFile jarFile = new JarFile("lib/a.jar"); InputStream in = jarFile.getInputStream(jarFile.getEntry("source/test.txt")); byte[] b = new byte[1024]; int len; while((len=in.read(b))!=-1){ System.out.println(new String(b,0,len)); }
比如“../source/1.txt”或者"source/1.txt"
试试看,多试几次肯定能成功
==============
我以前用类似这种写法, 在一般的java 应用程序中是可以访问, 但在tomcat + jsp 环境下,访问不了. 好象一般的java程序当前路径就是main()函数所在的jar包的路径,可以用./source/1.txt 这样访问. 如果是jsp程序,当前路径好象变成了tomcat的某个子目录下,用 "./source/1.txt " 这样的路径变成了访问tomcat目录下的source子目录,而并不是访问a.jar包内的source目录
JarEntry entry = jarFile.getJarEntry("source/a.txt");
InputStream in = jarFile.getInputStream(jarFile.getEntry("source/test.txt"));
byte[] b = new byte[1024];
int len;
while((len=in.read(b))!=-1){
System.out.println(new String(b,0,len));
}
pacakge.filename的方式访问~
就像普通的class文件那样访问~也就是用"包名.文件名",而不是"文件夹名/文件名"