从jar包中读取文件

以下的Java代码实现了从一个jar包中读取指定文件的功能:
 1ExpandedBlockStart.gif /**
 2InBlock.gif * This class implements the funcationality of reading and writing files in jar files.
 3ExpandedBlockEnd.gif */

 4None.gifpackage  com.leo.util;
 5
None.gif
 6None.gifimport
 java.io.InputStream;
 7None.gifimport
 java.io.FileOutputStream;
 8None.gifimport java.util.jar.*
;
 9None.gifimport
 java.util.Enumeration;
10
None.gif
11ExpandedBlockStart.gif
/**
12InBlock.gif * @author Leo Share
13InBlock.gif * @since
 08/09/2007
14InBlock.gif * @version
 1.0
15ExpandedBlockEnd.gif */

16 None.gif
17ExpandedBlockStart.gifContractedBlock.gifpublic class JarFileAccess 
{
18InBlock.gif    private static final String fileSeparator = System.getProperty("file.separator"
);
19
InBlock.gif    
20ExpandedSubBlockStart.gifContractedSubBlock.gif    public void accessJarFile(String jarFileName, String fromDir, String toDir) throws Exception
{
21InBlock.gif        JarFile myJarFile = new JarFile(fromDir+fileSeparator+
jarFileName);
22InBlock.gif        Enumeration myEnum =
 myJarFile.entries();
23ExpandedSubBlockStart.gifContractedSubBlock.gif        while(myEnum.hasMoreElements())
{
24InBlock.gif            JarEntry myJarEntry =
 (JarEntry)myEnum.nextElement();
25ExpandedSubBlockStart.gifContractedSubBlock.gif            if(myJarEntry.getName().equals("jbossall-client.jar"))
{
26InBlock.gif                InputStream is =
 myJarFile.getInputStream(myJarEntry);
27InBlock.gif                FileOutputStream fos = new FileOutputStream(toDir+fileSeparator+
myJarEntry.getName());
28InBlock.gif                byte[] b = new byte[1024
];
29InBlock.gif                int
 len;
30ExpandedSubBlockStart.gifContractedSubBlock.gif                while((len = is.read(b))!= -1)
{
31InBlock.gif                    fos.write(b, 0
, len);
32ExpandedSubBlockEnd.gif                }

33InBlock.gif                fos.close();
34
InBlock.gif                is.close();
35InBlock.gif                break
;
36ExpandedSubBlockEnd.gif            }

37ExpandedSubBlockStart.gifContractedSubBlock.gif            else{
38InBlock.gif                continue
;
39ExpandedSubBlockEnd.gif            }

40ExpandedSubBlockEnd.gif        }

41InBlock.gif        myJarFile.close();
42ExpandedSubBlockEnd.gif    }

43ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/xxpal/articles/859231.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值