当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现从jar包中读取指定文件的方法

Java实现从jar包中读取指定文件的方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java实现从jar包中读取指定文件的方法。分享给大家供大家参考,具体如下:

以下的java代码实现了从一个jar包中读取指定文件的功能:

/**
* this class implements the funcationality of reading and writing files in jar files.
*/
package com.leo.util;
import java.io.inputstream;
import java.io.fileoutputstream;
import java.util.jar.*;
import java.util.enumeration;
/**
* @author leo share
* @since 08/09/2007
* @version 1.0
*/
public class jarfileaccess {
  private static final string fileseparator = system.getproperty("file.separator");
  public void accessjarfile(string jarfilename, string fromdir, string todir) throws exception{
    jarfile myjarfile = new jarfile(fromdir+fileseparator+jarfilename);
    enumeration myenum = myjarfile.entries();
    while(myenum.hasmoreelements()){
      jarentry myjarentry = (jarentry)myenum.nextelement();
      if(myjarentry.getname().equals("jbossall-client.jar")){
        inputstream is = myjarfile.getinputstream(myjarentry);
        fileoutputstream fos = new fileoutputstream(todir+fileseparator+myjarentry.getname());
        byte[] b = new byte[1024];
        int len;
        while((len = is.read(b))!= -1){
          fos.write(b, 0, len);
        }
        fos.close();
        is.close();
        break;
      } else{
        continue;
      }
    }
    myjarfile.close();
  }
}

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网