【发布时间】:2014-03-10 03:42:12
【问题描述】:
我想用 txt 文件创建一个 zip 文件。每个 txt 文件都是一个字符串。这是我的代码
File f = new File("test.zip");
f.mkdirs();
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
out.putNextEntry(new ZipEntry("testfolder/mytext.txt"));
byte[] data = stringInTXT.toString().getBytes();
out.write(data, 0, data.length);
out.closeEntry();
out.close();
} catch (IOException e) {
System.out.println(e.toString());
}
我得到一个 FileNotFoundException
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
这是用 txt 文件创建 zip 文件的正确方法吗?如果是这样,我将如何解决此异常?
【问题讨论】:
-
请确认文件存在!!通过在 if 块中检查目录/文件是否存在,然后以这样的方式进行,如果目录/文件不存在而不是创建一个新目录/文件,请始终使用完整路径作为资源之类的路径!