如何在不关闭Java GUI的情况下释放文件

凯文

我创建了一个非常简单的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);
            }
            bos.flush();
            bos.close();
            fos.close();
            is.close();
        }
        zipFile.close();
    }catch(IOException ioe){
        ioe.printStackTrace();
    }finally{
        try{
            if(bos != null){
                bos.close();
            }
            if(fos != null) {
                fos.close();
            }
            if(is != null){
                is.close();
            }
            if(zipFile != null){
                zipFile.close();
            }

        }catch(Exception e) {
            e.printStackTrace();
        }
        return topLevelDirName;
    }
}

但是,除非明确关闭GUI,否则我仍然无法删除该zip。

想知道是否与Windows文件句柄有关吗?

疯狂程序员

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。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在不关闭流的情况下从流量收集

如何在不关闭xterm的情况下捕获SIGINT?

如何在不关闭SAS的情况下停止它?

如何在不关闭GUI窗口的情况下停止运行PyQt5程序?

如何在不关闭当前文件的情况下保存无宏副本?

如何在不关闭的情况下保存和读取c ++ fstream文件

如何在不关闭 Gvim 中的当前文件的情况下转到不同文件中的标记

批处理文件:如何在不关闭命令提示符的情况下执行cmd命令?

如何在不关闭显示器的情况下启用屏幕锁定?(Ubuntu Gnome 14.04)

如何在不关闭程序的情况下重新启动我的游戏

如何在不关闭命令终端窗口的情况下终止“ Ping”?

如何在不关闭窗口的情况下进行网格更新

如何在不关闭主程序的情况下终止Firefox中的子进程

如何在不关闭Gitlab中的问题的情况下合并分支?

如何在不关闭电源的情况下更改虚拟机中的RAM数量

如何在不关闭套接字的情况下将FIN标志发送到主机

如何在不关闭的情况下构建和初始化

如何在不关闭正在运行的ssh / telnet会话的情况下退出shell脚本

如何在素面调用ajax后不关闭组件的情况下更新p:selectCheckboxMenu的标签?

如何在不关闭OpenCV漏洞的情况下扩展二进制图像

如何在不关闭的情况下删除列表视图项

如何在不关闭Julia环境的情况下清洁地块(GR)

PyQt - 如何在不关闭对话框窗口的情况下停止执行

如何在不关闭VS Code的情况下重置Python分析器?

Kivy:如何在不关闭弹出窗口的情况下更新弹出标签文本

如何在不关闭终端的情况下运行脚本?

如何在不关闭底层 Web 套接字的情况下将 `firstValueFrom` 与 `WebSocketSubject` 结合使用?

我如何在不关闭konsole窗口的情况下杀死ssh卡住的连接?

如何在不关闭/禁用的情况下使一个屏幕变黑?