qq_61294100 2024-08-19 00:40 采纳率: 76%
浏览 4
已结题

合并分片文件后无法删除分片文件

背景:
合并分片文件后删除所有分片文件
问题:
使用try-catch-finally语句 在finally语句中执行删除操作,但是一直报无法删除文件的错误
改成了try with resource语句 就没有问题了 可以正常删除

这是try-catch-finally方式的代码:

        RandomAccessFile write = null;
        RandomAccessFile read = null;
        try {
            write = new RandomAccessFile(toFilePath, "rw");
            File[] filesDir = dirPath.listFiles();
            byte[] array = new byte[1024 * 10];
            for (int i = 0; i < filesDir.length; i++) {
                File file = new File(dirPath.getPath() + File.separator + i);
                read = new RandomAccessFile(file, "r");


                int len = -1;
                while ((len = read.read(array)) != -1) {
                    write.write(array, 0, len);
                }
            }
        } catch (Exception e) {
            logger.error("合并文件:{}失败", fileName, e);
            throw new BusinessException("合并文件" + fileName + "失败");

        } finally {
            if (null != write) {
                try {
                    write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != read) {
                try {
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //删除合并前的文件

            if (delSource && dirPath.exists()) {
                try {
                    FileUtils.deleteDirectory(dirPath);
                } catch (Exception e) {
                    logger.error("文件删除失败:", e);
                }
            }

        }

这是try with resource方式的代码

try (RandomAccessFile write = new RandomAccessFile(toFilePath, "rw")) {
            File[] filesDir = dirPath.listFiles();
            byte[] array = new byte[1024 * 10];
            for (int i = 0; i < filesDir.length; i++) {
                File file = new File(dirPath.getPath() + File.separator + i);
                try (RandomAccessFile read = new RandomAccessFile(file, "r")) {
                    int len = -1;
                    while ((len = read.read(array)) != -1) {
                        write.write(array, 0, len);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("合并文件:{}失败", fileName, e);
            throw new BusinessException("合并文件" + fileName + "失败");

        } finally {
            //删除合并前的文件

            if (delSource && dirPath.exists()) {
                try {
                    FileUtils.deleteDirectory(dirPath);
                } catch (Exception e) {
                    logger.error("文件删除失败:", e);
                }
            }

        }


求解答

  • 写回答

4条回答 默认 最新

  • micthis 2024-08-19 09:14
    关注

    因为你以"读"方式打开的文件有多个,用try-catch-finally语句只关闭了最后一个而用try with resource语句都关闭了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月29日
  • 已采纳回答 8月21日
  • 创建了问题 8月19日