zoukankan      html  css  js  c++  java
  • jar包的读取1

    ---恢复内容开始---

    昨天在做项目插件的时候,因为会用到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 }

    ---恢复内容结束---

  • 相关阅读:
    剑指Offer-30.连续子数组的最大和(C++/Java)
    剑指Offer-29.最小的K个数(C++/Java)
    UVA 1616 Caravan Robbers 商队抢劫者(二分)
    UVA 10570 Meeting with Aliens 外星人聚会
    UVA 11093 Just Finish it up 环形跑道 (贪心)
    UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)
    UVA 10954 Add All 全部相加 (Huffman编码)
    UVA 714 Copying Books 抄书 (二分)
    UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
    codeforecs Gym 100286B Blind Walk
  • 原文地址:https://www.cnblogs.com/lihuibin/p/9122292.html
Copyright © 2011-2022 走看看