Java_使用jarFile读取jar包中的内容
来源:互联网 发布:网络专供是真的假的 编辑:程序博客网 时间:2024/06/06 06:56
jarFile是Java提供的一个类,可以读取jar包中的所有文件(递归读取),然后提取指定的文件
使用绝对路径来举例
假定D盘下有一个jar包:test.jar,现将jar包中resource目录下的aa.txt文件拷贝到项目的同级目录名为test的文件夹下,resource目录下的res文件夹拷贝到test文件夹下res文件夹下。
大概就是这种操作:
D:/test.jar/.../resource/aa.txt ----> D:/test/aa.txtD:/test.jar/.../resource/res/* ----> D:/test/res/*
代码如下:
public class TestJarFile{ //做一些预处理,比如获取当前项目路径,和创建文件夹的操作 private String projectPath; private String targetDirPath; private String resPath; { projectPath = System.getProperty("user.dir");//当前项目路径, targetDirPath = projectPath + File.separator + "test";//使用File提供的分隔符,区分windows和linux File file = new File(targetDirPath); if(!file.exists()){ file.mkdirs(); } resPath = targetDirPath + File.separator + "res"; // res文件夹 File f = new File(resPath); if(!f.exists()){ f.mkdirs(); } } public void writeJarFile() throws Exception{ InputStream is = null; String jarFilePath = projectPath + File.separator + "test.jar"; JarFile jarFile = new JarFile(jarFilePath); for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements(); ) { //这个循环会读取jar包中所有文件,包括文件夹 JarEntry jarEntry = e.nextElement();//jarEntry就是我们读取的jar包中每一个文件了,包括目录 if(jarEntry.getName().contains("resource/aa.txt"){ //getName()会获取文件全路径名称 //如果是aa.txt就将其拷贝到test文件夹下 is = jarFile.getInputStream(jarEntry); //将目标文件读到流中 String targetFileName = targetDirPath + File.separator + "aa.txt"; File file = new File(targetFileName); //我们自己手写一个方法,用来读写文件 writeFile(is, file); }else if (jarEntry.getName().contains("resource/res/")){ //读取res文件夹和res文件夹下的所有文件, //在读取aa.txt文件时,投机取巧了一下,直接写死为aa.txt,但是循环读取多个文件,不知道文件名,需要截取目标文件的名字 String[] split = jarEntry.getName().split("/"); jar包中都是以 "/" 分割的 String targetFileName = resPath + File.separator + split[split.length - 1]; //最后一位就是文件的名字 File file = new File(targetFileName); //注意,因为这个if判断会读取res文件夹和res文件夹下的文件,因此需要区分 if(jarEntry.isDirtory()){ continue; }else { is = jarFile.getInputStream(jarEntry); writeFile(is, file); } } } if (is != null){ is.close(); } } public void writeFile(InputStream is, File file) throws Exception{ if(file != null){ //推荐使用字节流读取,因为虽然读取的是文件,如果是 .exe, .c 这种文件,用字符流读取会有乱码 OutputStream os = new BufferedOutputStream(new FileOutputStream(file)); byte[] bytes = new byte[2048]; //这里用小数组读取,使用file.length()来一次性读取可能会出错(亲身试验) int len; while((len = is.read(bytes)) != -1) { os.write(bytes, 0, len); } os.close(); } }}
过程大概就这些,如果加log日志,自己加就行了
阅读全文
0 0
- Java_使用jarFile读取jar包中的内容
- jarFile读取jar文件内容
- JarFile读取jar内文件实例
- 读取jar包中的文件
- 读取Jar包中的配置文件
- java读取jar包里文件内容
- jar包中读取文件及其内容
- 执行jar包时出现invalid or corrupt jarfile
- hadoop yarn运行jar包 出错 RunJar jarFile [mainClass] args...
- 如何修改jar包中的内容
- 读取jar包中的properties文件
- 读取Jar包中的资源问题探究
- 读取jar包中的资源文件
- 读取Jar包中的资源问题探究
- 读取Jar包中的资源问题探究
- 读取Jar包中的资源问题探究
- Java中读取jar包中的文件
- 修改jar包中的内容并把新内容打到jar包中
- 关于微网站IOS调用微信扫码问题
- 开启snapshot的操作失败 如何解决——两种办法
- .net面试
- [BZOJ]2160 拉拉队排练 Manacher+快速幂
- Java_基础—对象操作流ObjecOutputStream/ObjectInputStream
- Java_使用jarFile读取jar包中的内容
- Linux 下文件拷贝
- 为eclipse中的文件设置默认编码
- 排序
- http://download.java.net/openjdk/jdk7下载openjdk源码的网站的bug
- C++中括号()操作符的重载举例及解析
- JavaScript标准教程
- CentOS7.2下VSFTPD的安装、配置、卸载
- SpingBoot项目打war包部署到其他tomcat中