本文实例讲述了Java实现从jar包中读取指定文件的方法。分享给大家供大家参考,具体如下:
以下的Java代码实现了从一个jar包中读取指定文件的功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/** * 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程序设计有所帮助。
原文链接:http://www.cnblogs.com/xxpal/articles/859231.html