我正在寫代碼來創建txt文件,並且完全寫入該txt文件後意味着完全關閉txt文件而不是zip文件。但是,我不爲什麼,就不會等到文件關閉文件之前關閉它壓縮它.. 請幫我完整寫入文件後的Java代碼壓縮文件
這裏是我的代碼:
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class zipfile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedWriter bfAllBWPownmanmainfest = null;
String mainfest = "file\\" + "fileforzip" + ".txt";
bfAllBWPownmanmainfest = new BufferedWriter(new FileWriter(mainfest));
bfAllBWPownmanmainfest.write("jdshsdksdkhdshksd\n");
bfAllBWPownmanmainfest.write("jdshsdksdkhsdfsdfsddshksd\n");
bfAllBWPownmanmainfest.write("jdshsdksdsdfdskhdshksd\n");
bfAllBWPownmanmainfest.write("jdshsdksddsfdskhdshksd\n");
bfAllBWPownmanmainfest.write("jdshsdksddsfdskhdshksd\n");
bfAllBWPownmanmainfest.write("jdshsdksdsdfdskhdshksd\n");
bfAllBWPownmanmainfest.write("jdshsdksddsfsdkhdshksd\n");
bfAllBWPownmanmainfest.flush();
bfAllBWPownmanmainfest.close();
//After close file than zip that!! please help me Thanks
FileOutputStream fout = new FileOutputStream("test.zip");
ZipOutputStream zout = new ZipOutputStream(fout);
ZipEntry ze = new ZipEntry(mainfest);
zout.putNextEntry(ze);
zout.closeEntry();
zout.close();
}
}
接近bfAllBWPownmanmainfest.close後();比拉鍊,我怎麼能做到這一點,請高級幫助我感謝!它創建空的zip文件,它並沒有等到文件完全關閉! 請幫助我!謝謝!!
只是一個fyi,關閉刷新緩衝區,所以調用刷新然後關閉是多餘的。 – Grammin
參考這個http://stackoverflow.com/questions/1091788/how-to-create-a-zip-file-in-java 你必須寫信給ZipOutputStream。 – sri