用java代码把txt文件自动打包到jar文件中,如何做? 谢谢!

用java代码把txt文件自动打包到jar文件中,如何做? 谢谢!

第1个回答  2010-07-27
如认可,请您赏分30分,谢谢
命令行参数:
args[0] 待打包的完整文件路径
args[1] jar 输出文件的完整路径

import java.io.*;
import java.util.zip.*;
/**
* @author Hardneedl
*/
public class JarPacker {
public static void main(String[] args) {
File inFile = new File(args[0]);
ZipEntry zipEntry = new ZipEntry (inFile.getName());

try {
InputStream ins = new FileInputStream(inFile);
byte[]datas = new byte[ins.available()];
ins.read(datas);
ins.close();

ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(args[1]));
zout.putNextEntry(zipEntry);
zout.write(datas);
zout.closeEntry();
zout.finish();
zout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
相似回答