常用鏈接

          統(tǒng)計

          最新評論

          從jar中讀取文件

          Sample1,利用Menifest文件讀取jar中的文件
          /*
          1.文件目錄
          test--
               --a.text
               --b.gif

          2. Menifest文件內(nèi)容:
          Manifest-Version: 1.0
          abc: test/a.txt
          iconname: test/Anya.jpg
          注意:manifest.mf文件最后一行要打一回車
          Another Notification:
          如果manifest文件內(nèi)容是:
          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包相對的路徑,發(fā)布時把jar包和其他用到的類庫一起交給用戶就可以了。


          3.打jar包
          test.jar
          */ 
          String iconpath = jar.getManifest().getMainAttributes().getValue("abc");
              InputStream in = jar.getInputStream(jar.getJarEntry(iconpath));
               //Image img = ImageIO.read(in);
              InputStreamReader isr =  new InputStreamReader(in);
                 BufferedReader reader = new BufferedReader(isr);
                 String line;
                 while ((line = reader.readLine()) != null) {
                     System.out.println(line);
                 }
                 reader.close();

          Sample2,讀取JAR 文件列表及各項的名稱、大小和壓縮后的大小

          public class JarFileInfoRead {
           public static void main (String args[])
            throws IOException {
            String jarpath="d://temp//test.jar";
            JarFile jarFile = new JarFile(jarpath);
            Enumeration enu = jarFile.entries();
            while (enu.hasMoreElements()) {
                process(enu.nextElement());
            }
          }

          private static void process(Object obj) {
            JarEntry entry = (JarEntry)obj;
            String name = entry.getName();
            long size = entry.getSize();
            long compressedSize = entry.getCompressedSize();
            System.out.println(name + "\t" + size + "\t" + compressedSize);
          }
          }

          Sample3,讀取JAR中 文件的內(nèi)容
          public class JarFileRead {
              public static void main (String args[])
                  throws IOException {
                String jarpath="d://temp//test.jar";
                JarFile jarFile = new JarFile(jarpath);
                  Enumeration enu = jarFile.entries();
                  while (enu.hasMoreElements()) {
                   JarEntry entry = (JarEntry)enu.nextElement();
                      String name = entry.getName();
                      //System.out.println(name);
                      if(name.equals("test/a.txt")){
                      InputStream input = jarFile.getInputStream(entry);
                      process(input);
                      }
                  }       
                  jarFile.close();
              }
              private static void process(InputStream input)
                  throws IOException {
                  InputStreamReader isr =
                      new InputStreamReader(input);
                  BufferedReader reader = new BufferedReader(isr);
                  String line;
                  while ((line = reader.readLine()) != null) {
                      System.out.println(line);
                  }
                  reader.close();
              }
          }

          posted on 2008-03-06 12:51 九寶 閱讀(903) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 汶上县| 冀州市| 荆州市| 依兰县| 兰西县| 河北省| 普安县| 白山市| 崇阳县| 华池县| 辽宁省| 万州区| 韩城市| 白山市| 安泽县| 清徐县| 博湖县| 牡丹江市| 资兴市| 万年县| 桂阳县| 和龙市| 视频| 炎陵县| 玉环县| 镇坪县| 通城县| 穆棱市| 天峨县| 饶河县| 会理县| 肃宁县| 天等县| 望奎县| 鲁甸县| 策勒县| 平谷区| 太仓市| 萝北县| 龙岩市| 开化县|