jar包的读取

昨天在做项目插件的时候,因为会用到jar包中的一个文件来初始化程序。并且以后还是会访问这个文件,所以就想到干脆吧文件拷贝到指定目录。在拷贝的时候也费了好一会时间,这里涉及到了jar文件的操作,在这里记下来以后有用到的时候方便查找

  1. 如果jar中还存在jar包或者其他压缩包,则使用这种方式读取
    1.  1 public class JarFileAccess {
       2 
       3     private static final String fileSeparator = System.getProperty("file.separator");
       4     /**
       5      * 
       6      * @param jarFileName jar文件的名称,(注意要添加“.jar”后缀,不要加任何路径分隔符)
       7      * @param fromDir jar的路径
       8      * @param toDir 要将文件拷贝到指定位置的路径
       9      * @throws Exception
      10      */
      11     public void accessJarFile(String jarFileName, String fromDir, String toDir) throws Exception{
      12     JarFile myJarFile = new JarFile(fromDir+fileSeparator+jarFileName);
      13     Enumeration myEnum = myJarFile.entries();
      14     while(myEnum.hasMoreElements()){
      15       JarEntry myJarEntry = (JarEntry)myEnum.nextElement();
      16       System.out.println(myJarEntry.getName());
      17       if(myJarEntry.getName().equals("config.jar")){
      18         InputStream is = myJarFile.getInputStream(myJarEntry);
      19         FileOutputStream fos = new FileOutputStream(toDir+fileSeparator+myJarEntry.getName());
      20         byte[] b = new byte[1024];
      21         int len;
      22         while((len = is.read(b))!= -1){
      23             System.out.println(b.toString());
      24           fos.write(b, 0, len);
      25         }
      26         fos.close();
      27         is.close();
      28         break;
      29       } else{
      30         continue;
      31       }
      32     }
      33     myJarFile.close();
      34   }
      35 }

       

  2. 如果要读取的文件在jar包中不是以压缩包或jar的形式存在,用下面的方式方便点
    1.  1 public class JarFileAccess{
       2 /**
       3        * @function 读取jar包中指定文件的内容并且以字符串形式返回
       4        * @param jarPath jar文件的路径
       5        * @param name 要读取的文件名称,要添加后缀名
       6        * @return String 返回读取到的信息
       7        * @throws IOException
       8        */
       9       public  String readFileFromJar(String jarPath ,String name) throws IOException {  
      10             JarFile jf = new JarFile(jarPath);  
      11             Enumeration<JarEntry> jfs = jf.entries();  
      12             StringBuffer sb  = new StringBuffer();  
      13             while(jfs.hasMoreElements())  
      14             {  
      15                 JarEntry jfn = jfs.nextElement();  
      16                 if(jfn.getName().endsWith(name))  
      17                 {  
      18                     InputStream is = jf.getInputStream(jfn);  
      19                     BufferedInputStream bis = new BufferedInputStream(is);  
      20                     byte[] buf = new byte[is.available()];  
      21                     while(bis.read(buf)!=-1)  
      22                     {  
      23                         sb.append(new String(buf).trim());  
      24                     }  
      25                     bis.close();  
      26                     is.close();  
      27                     break;  
      28                 }  
      29             }  
      30             return sb.toString();  
      31         }  
      32       /**
      33        * @function 读取jar包中指定文件的内容并且将读取到的内容拷贝到指定文件中
      34        * @param jarPath jar文件的路径
      35        * @param name 要读取的文件名称,要添加后缀名
      36        * @param toNewFile 将拷贝到的信息复制到目标文件
      37        * @throws IOException
      38        */
      39       public  void readFileFromJar(String jarPath ,String name,File toNewFile) throws IOException {  
      40             JarFile jf = new JarFile(jarPath);  
      41             Enumeration<JarEntry> jfs = jf.entries();  
      42             StringBuffer sb  = new StringBuffer();
      43             while(jfs.hasMoreElements())  
      44             {  
      45                 JarEntry jfn = jfs.nextElement();  
      46                 if(jfn.getName().endsWith(name))  
      47                 {  
      48                     InputStream is = jf.getInputStream(jfn);
      49                     FileOutputStream fos = new FileOutputStream(toNewFile);
      50                     BufferedInputStream bis = new BufferedInputStream(is);  
      51                     byte[] buf = new byte[is.available()];  
      52                     while(bis.read(buf)!=-1)  
      53                     {  
      54                         fos.write(buf);
      55                        
      56                     }  
      57                     fos.close();
      58                     bis.close();  
      59                     is.close();  
      60                     break;  
      61                 }  
      62             }  
      63            
      64         }  
      65       
      66 }

       

转载于:https://www.cnblogs.com/lihuibin/p/9121012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值