如何在不关闭的情况下在 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);

            }

           

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


想知道是否与 Windows 文件句柄有关?提前致谢。


慕婉清6462132
浏览 230回答 1
1回答

DIEA

Java 8 引入了try-with-resources 语句,使这种情况更简单、更清晰。您遇到的问题之一是,如果关闭您打开的许多资源的任何一种尝试失败,那么其他任何资源都不会被关闭public static String unZip(String path) throws IOException {&nbsp; &nbsp; int count = -1;&nbsp; &nbsp; File sourceFile = new File(path);&nbsp; &nbsp; String name = sourceFile.getName();&nbsp; &nbsp; name = name.substring(0, name.lastIndexOf(".zip"));&nbsp; &nbsp; File sourcePath = new File(sourceFile.getParent(), name);&nbsp; &nbsp; System.out.println("SavePath = " + sourcePath);&nbsp; &nbsp; if (!sourcePath.exists() && !sourcePath.mkdirs()) {&nbsp; &nbsp; &nbsp; &nbsp; throw new IOException("Could not create directory " + sourcePath);&nbsp; &nbsp; }&nbsp; &nbsp; String topLevelDirName = "";&nbsp; &nbsp; try (ZipFile zipFile = new ZipFile(sourceFile)) {&nbsp; &nbsp; &nbsp; &nbsp; Enumeration<?> entries = zipFile.entries();&nbsp; &nbsp; &nbsp; &nbsp; int levelCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; byte buf[] = new byte[1024];&nbsp; &nbsp; &nbsp; &nbsp; while (entries.hasMoreElements()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ZipEntry entry = (ZipEntry) entries.nextElement();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String filename = entry.getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File file = new File(sourcePath, filename);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (entry.isDirectory()) { //If it is a directory&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; levelCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Make directory " + file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!file.exists() && !file.mkdirs()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IOException("Could not create directory " + filename);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Extract to " + file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try (InputStream is = zipFile.getInputStream(entry);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((count = is.read(buf)) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bos.write(buf, 0, count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return topLevelDirName;}我已经稍微更新了代码,以尝试使其更清晰、更简单,并利用可用的 API
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java