java 从jar中读取文件 三种方法<Enumeration>

  • Sample1-利用Manifest文件读取jar中的文件

1.文件目录
test--
     --a.text
     --b.gif

2. Menifest文件内容:
Manifest-Version: 1.0
abc: test/a.txt
iconname: test/Anya.jpg
注意:manifest.mf文件最后一行要打一回车
Another Notification:
如果manifest文件内容是:
Manifest-Version: 1.0
Main-Class: com.DesignToolApp
Class-path: lib/client.jar lib/j2ee.jar
在MANIFEST.MF文件的最后,要留两个空行(也就是回车),才可以识别到Class-Path这一行,如果只有一个空行,那么只识别到Main- Class这一行。Class-Path中的库名用空格格开,使用和jar包相对的路径,发布时把jar包和其他用到的类库一起交给用户就可以了。


3.打jar包
test.jar

Java代码   收藏代码
  1. String iconpath = jar.getManifest().getMainAttributes().getValue("abc");  
  2. InputStream in = jar.getInputStream(jar.getJarEntry(iconpath));  
  3.  //Image img = ImageIO.read(in);  
  4. InputStreamReader isr =  new InputStreamReader(in);  
  5. BufferedReader reader = new BufferedReader(isr);  
  6. String line;  
  7. while ((line = reader.readLine()) != null) {  
  8.       System.out.println(line);  
  9. }  
  10. reader.close();  
 

 

  • Sample2,读取JAR 文件列表及各项的名称、大小和压缩后的大小

 

Java代码   收藏代码
  1. public class JarFileInfoRead {  
  2.  public static void main (String args[])  throws IOException {  
  3.   String jarpath="d://temp//test.jar";  
  4.   JarFile jarFile = new JarFile(jarpath);  
  5.   Enumeration enu = jarFile.entries();  
  6.   while (enu.hasMoreElements()) {  
  7.       process(enu.nextElement());  
  8.   }  
  9. }  
  10.   
  11. private static void process(Object obj) {  
  12.     JarEntry entry = (JarEntry)obj;  
  13.     String name = entry.getName();  
  14.     long size = entry.getSize();  
  15.     long compressedSize = entry.getCompressedSize();  
  16.     System.out.println(name + "\t" + size + "\t" + compressedSize);  
  17.   }  
  18. }  
 

 

  • Sample3,读取JAR中 文件的内容
Java代码   收藏代码
  1. public class JarFileRead {  
  2.     public static void main (String args[])  
  3.         throws IOException {  
  4.       String jarpath="d://temp//test.jar";  
  5.       JarFile jarFile = new JarFile(jarpath);  
  6.         Enumeration enu = jarFile.entries();  
  7.         while (enu.hasMoreElements()) {  
  8.          JarEntry entry = (JarEntry)enu.nextElement();  
  9.             String name = entry.getName();  
  10.             //System.out.println(name);  
  11.             if(name.equals("test/a.txt")){  
  12.             InputStream input = jarFile.getInputStream(entry);  
  13.             process(input);  
  14.             }  
  15.         }         
  16.         jarFile.close();  
  17.     }  
  18.     private static void process(InputStream input)  
  19.         throws IOException {  
  20.         InputStreamReader isr =  
  21.             new InputStreamReader(input);  
  22.         BufferedReader reader = new BufferedReader(isr);  
  23.         String line;  
  24.         while ((line = reader.readLine()) != null) {  
  25.             System.out.println(line);  
  26.         }  
  27.         reader.close();  
  28.     }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
通过 `classLoader.getResources(path)` 获取到的是一个URL列表,其包含了符合指定路径的所有资源文件的URL,包括位于jar的资源文件的URL。我们可以通过遍历这个URL列表,获取到每个资源文件的URL,然后使用JavaJarURLConnection类来读取jar的资源文件。 下面是一个示例代码: ```java ClassLoader classLoader = getClass().getClassLoader(); Enumeration<URL> resources = classLoader.getResources("com/example/config.properties"); while (resources.hasMoreElements()) { URL url = resources.nextElement(); if (url.getProtocol().equals("file")) { // 处理普通文件 File file = new File(url.toURI()); // 读取文件内容 } else if (url.getProtocol().equals("jar")) { // 处理jar的文件 JarURLConnection connection = (JarURLConnection) url.openConnection(); InputStream inputStream = connection.getInputStream(); // 读取文件内容 } } ``` 以上代码,我们首先通过 `classLoader.getResources(path)` 获取到了符合指定路径的所有资源文件的URL,然后通过 `hasMoreElements()` 和 `nextElement()` 方法遍历这个URL列表,对于每个URL,判断它的协议是 `file` 还是 `jar`。如果是 `file` 协议,说明该资源文件是一个普通文件,我们可以使用 `File` 类获取到该文件的路径,然后读取文件内容即可。如果是 `jar` 协议,说明该资源文件位于jar,我们可以通过 `JarURLConnection` 类获取到该文件的输入流,然后读取文件内容即可。需要注意的是,读取文件时,要根据文件的类型进行不同的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值