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 }

       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM