jar文件如何向jar的外部文件写入数据,比如说D盘的某个目录
OutputStream out= new FileOutputStream("D://info.txt");out.write(new String("hello").getBytes());
在eclipse中能够向info.txt文件中写入数据,打包成jar文件后无法写入数据????? --------------------编程问答-------------------- 可变的不要放到jar里。 --------------------编程问答-------------------- JarInputStream
JarOutputStream
public class Util_Jar_Test {
Logger log = Logger.getLogger(this.getClass());
//复制jar
public void copyJar(File src , File des) throws FileNotFoundException, IOException{
JarInputStream jarIn = new JarInputStream(new BufferedInputStream(new FileInputStream(src)));
Manifest manifest = jarIn.getManifest();
JarOutputStream jarOut = null;
if(manifest == null){
jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(des)));
}else{
jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(des)),manifest);
}
byte[] bytes = new byte[1024];
while(true){
//重点
ZipEntry entry = jarIn.getNextJarEntry();
if(entry == null)break;
jarOut.putNextEntry(entry);
int len = jarIn.read(bytes, 0, bytes.length);
while(len != -1){
jarOut.write(bytes, 0, len);
len = jarIn.read(bytes, 0, bytes.length);
}
log.info("Copyed: " + entry.getName());
// jarIn.closeEntry();
// jarOut.closeEntry();
String a = new String();
a.length();
}
jarIn.close();
jarOut.finish();
jarOut.close();
}
//解压jar
public void unJar(File src , File desDir) throws FileNotFoundException, IOException{
JarInputStream jarIn = new JarInputStream(new BufferedInputStream(new FileInputStream(src)));
if(!desDir.exists())desDir.mkdirs();
byte[] bytes = new byte[1024];
while(true){
ZipEntry entry = jarIn.getNextJarEntry();
if(entry == null)break;
File desTemp = new File(desDir.getAbsoluteFile() + File.separator + entry.getName());
if(entry.isDirectory()){ //jar条目是空目录
if(!desTemp.exists())desTemp.mkdirs();
log.info("MakeDir: " + entry.getName());
}else{ //jar条目是文件
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desTemp));
int len = jarIn.read(bytes, 0, bytes.length);
while(len != -1){
out.write(bytes, 0, len);
len = jarIn.read(bytes, 0, bytes.length);
}
out.flush();
out.close();
log.info("Copyed: " + entry.getName());
}
jarIn.closeEntry();
}
//解压Manifest文件
Manifest manifest = jarIn.getManifest();
if(manifest != null){
File manifestFile = new File(desDir.getAbsoluteFile()+File.separator+JarFile.MANIFEST_NAME);
if(!manifestFile.getParentFile().exists())manifestFile.getParentFile().mkdirs();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(manifestFile));
manifest.write(out);
out.close();
}
//关闭JarInputStream
jarIn.close();
}
//复制jar by JarFile
public void copyJarByJarFile(File src , File des) throws IOException{
//重点
JarFile jarFile = new JarFile(src);
Enumeration<JarEntry> jarEntrys = jarFile.entries();
JarOutputStream jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(des)));
byte[] bytes = new byte[1024];
while(jarEntrys.hasMoreElements()){
JarEntry entryTemp = jarEntrys.nextElement();
jarOut.putNextEntry(entryTemp);
BufferedInputStream in = new BufferedInputStream(jarFile.getInputStream(entryTemp));
int len = in.read(bytes, 0, bytes.length);
while(len != -1){
jarOut.write(bytes, 0, len);
len = in.read(bytes, 0, bytes.length);
}
in.close();
jarOut.closeEntry();
log.info("Copyed: " + entryTemp.getName());
}
jarOut.finish();
jarOut.close();
jarFile.close();
}
//解压jar文件by JarFile
public void unJarByJarFile(File src , File desDir) throws FileNotFoundException, IOException{
JarFile jarFile = new JarFile(src);
Enumeration<JarEntry> jarEntrys = jarFile.entries();
if(!desDir.exists())desDir.mkdirs(); //建立用户指定存放的目录
byte[] bytes = new byte[1024];
while(jarEntrys.hasMoreElements()){
ZipEntry entryTemp = jarEntrys.nextElement();
File desTemp = new File(desDir.getAbsoluteFile() + File.separator + entryTemp.getName());
if(entryTemp.isDirectory()){ //jar条目是空目录
if(!desTemp.exists())desTemp.mkdirs();
log.info("makeDir" + entryTemp.getName());
}else{ //jar条目是文件
//因为manifest的Entry是"META-INF/MANIFEST.MF",写出会报"FileNotFoundException"
File desTempParent = desTemp.getParentFile();
if(!desTempParent.exists())desTempParent.mkdirs();
BufferedInputStream in = new BufferedInputStream(jarFile.getInputStream(entryTemp));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desTemp));
int len = in.read(bytes, 0, bytes.length);
while(len != -1){
out.write(bytes, 0, len);
len = in.read(bytes, 0, bytes.length);
}
in.close();
out.flush();
out.close();
log.info("Copyed: " + entryTemp.getName());
}
}
jarFile.close();
}
参照:http://www.cnblogs.com/mailingfeng/archive/2011/07/30/2122160.html --------------------编程问答-------------------- 楼上的很囧,似乎没看懂楼主的意思
楼主的问题跟代码什么的没有关系,我用同样的代码和打成可执行的jar文件都正常执行,楼主之所以执行失败肯定是jar包没有成功打包,导致的你写的程序根本没有被执行
纯手工打jar包的时候一定要有mf文件制定main-class 为你想要执行的类,其实如果用JDK的类库也可以用eclipse生成可执行的jar包,方法参照http://blog.csdn.net/wbchn/article/details/2422137
另外,从程序设计来看,建议采用配置文件的方法,尽量不要让文件输出路径被封死在jar当中 --------------------编程问答--------------------
如果你的Jar包是用java -jar xxx.jar这种形式运行的,应该可以顺利开文件。
如果你把Jar包放applet中用,或放tomcat中去调用,记得修改policy文件,将该目录的IO权限赋予你的程序。Tomcat下是catalina.policy文件。 --------------------编程问答--------------------
我是用ant工具打包生成的jar文件,jar文件能够执行,在
OutputStream out= new FileOutputStream("D://info.txt");
out.write(new String("hello").getBytes());两条语句之前我写的是生成窗口的代码,能够成功生成窗口,只是运行jar文件不能向info.txt中写入数据。不知道你是否能明白我的意思???
--------------------编程问答--------------------
我是用ant工具打包生成的jar文件,jar文件能够执行,在
OutputStream out= new FileOutputStream("D://info.txt");
out.write(new String("hello").getBytes());两条语句之前我写的是生成窗口的代码,能够成功生成窗口,只是运行jar文件不能向info.txt中写入数据。不知道你是否能明白我的意思???
--------------------编程问答--------------------
如果你那里有问题,可能是
1.最可能是输出流没有关闭,没有执行out.close() ,所以写入的数据没有真正输出到文件
你加上out.close();这一行试试
2.写入数据之前,有异常抛出,这两行代码没有执行
--------------------编程问答-------------------- OutputStream out= new FileOutputStream("D://info.txt");
应该为OutputStream out= new FileOutputStream("D:/info.txt");
注意最后要关闭
可以out.flush();下 --------------------编程问答--------------------
有可能是7楼所说没flush和close
但这依然不能解释Eclipse中为什么可以运行出结果。
贴完整代码吧。
补充:Java , Java SE