亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在不關閉的情況下在 Java GUI 中釋放文件

如何在不關閉的情況下在 Java GUI 中釋放文件

慕婉清6462132 2021-09-12 14:27:06
我創建了一個非常簡單的 Java GUI 來瀏覽/加載 Windows 平臺上的 zip 文件以開始解壓縮,然后進行一些文件檢查。一切正常,除了我必須關閉 GUI 窗口才能刪除已在 GUI 中打開的 zip 文件。在解壓方法的 finally 塊中,我嘗試添加以下內容:public static String unZip(String path){    int count = -1;    String savepath = "";    File file = null;    InputStream is = null;    FileOutputStream fos = null;    BufferedOutputStream bos = null;    savepath = path.substring(0, path.lastIndexOf("\\")) + File.separator; //File saving directory    new File(savepath).mkdir(); //create the saving directory    ZipFile zipFile = null;    String topLevelDirName="";    try    {        zipFile = new ZipFile(path,Charset.forName("gbk")); //Encoding        Enumeration<?> entries = zipFile.entries();        int levelCount=0;        while(entries.hasMoreElements())        {            byte buf[] = new byte[buffer];            ZipEntry entry = (ZipEntry)entries.nextElement();            String filename = entry.getName();            boolean ismkdir = false;            if(filename.lastIndexOf("/") != -1){ //To check if there is a directory                ismkdir = true;            }            filename = savepath + filename;            if(entry.isDirectory()){ //If it is a directory                levelCount++;                file = new File(filename);                file.mkdirs();                if(levelCount==1)                    topLevelDirName = filename;                continue;            }            file = new File(filename);            if(!file.exists()){                if(ismkdir){                    new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs();                }            }            file.createNewFile(); //Create the file            is = zipFile.getInputStream(entry);            fos = new FileOutputStream(file);            bos = new BufferedOutputStream(fos, buffer);            while((count = is.read(buf)) > -1)            {                bos.write(buf, 0, count);            }           但是,除非明確關閉 GUI,否則我仍然無法刪除 zip。想知道是否與 Windows 文件句柄有關?提前致謝。
查看完整描述

1 回答

?
DIEA

TA貢獻1820條經驗 獲得超2個贊

Java 8 引入了try-with-resources 語句,使這種情況更簡單、更清晰。


您遇到的問題之一是,如果關閉您打開的許多資源的任何一種嘗試失敗,那么其他任何資源都不會被關閉


public static String unZip(String path) throws IOException {

    int count = -1;


    File sourceFile = new File(path);

    String name = sourceFile.getName();

    name = name.substring(0, name.lastIndexOf(".zip"));

    File sourcePath = new File(sourceFile.getParent(), name);


    System.out.println("SavePath = " + sourcePath);

    if (!sourcePath.exists() && !sourcePath.mkdirs()) {

        throw new IOException("Could not create directory " + sourcePath);

    }

    String topLevelDirName = "";

    try (ZipFile zipFile = new ZipFile(sourceFile)) {

        Enumeration<?> entries = zipFile.entries();

        int levelCount = 0;

        byte buf[] = new byte[1024];

        while (entries.hasMoreElements()) {

            ZipEntry entry = (ZipEntry) entries.nextElement();

            String filename = entry.getName();

            File file = new File(sourcePath, filename);

            if (entry.isDirectory()) { //If it is a directory

                levelCount++;

                System.out.println("Make directory " + file);

                if (!file.exists() && !file.mkdirs()) {

                    throw new IOException("Could not create directory " + filename);

                }

            } else {

                System.out.println("Extract to " + file);

                try (InputStream is = zipFile.getInputStream(entry);

                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {

                    while ((count = is.read(buf)) > -1) {

                        bos.write(buf, 0, count);

                    }

                }

            }

        }

    }

    return topLevelDirName;

}

我已經稍微更新了代碼,以嘗試使其更清晰、更簡單,并利用可用的 API


查看完整回答
反對 回復 2021-09-12
  • 1 回答
  • 0 關注
  • 248 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號