You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java替换Local AppData中图片遇DirectoryNotEmptyException的解决办法

解决Java代码替换文件时抛出DirectoryNotEmptyException的问题

问题原因

你遇到的DirectoryNotEmptyException是因为代码错误地将目录路径作为Files.copy的目标参数。原代码里获取的endPath是textures文件夹的路径,但你实际需要替换的是该文件夹下的ClassImages.png文件,直接向非空目录执行替换操作就会触发这个异常。

修正方案

修改Files.copy的目标路径为找到的目标文件的路径,而非其父目录。同时补充完善潜在的异常处理(比如Scanner资源泄漏、目录遍历的空指针问题)。

修正后的代码:

package robloxstudioclassimageloader;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;

public class RobloxStudioClassImageLoader {
    
    private static final Path SOURCE_IMAGE = Paths.get("resources/ClassImages.png");
    private static Scanner scanner;
    private static String robloxVersionsPath;
    
    public static void main(String[] args) {
        // 初始化Scanner并读取路径
        try {
            scanner = new Scanner(new File("resources/path.txt"));
            robloxVersionsPath = scanner.nextLine();
        } catch (FileNotFoundException e) {
            System.err.println("找不到路径文件:" + e.getMessage());
            System.exit(1);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        File versionsDir = new File(robloxVersionsPath);
        if (!versionsDir.isDirectory()) {
            System.err.println("指定的路径不是有效目录");
            System.exit(1);
        }

        // 遍历Versions目录下的版本文件夹
        File[] versionFolders = versionsDir.listFiles();
        if (versionFolders == null) {
            System.err.println("无法访问Versions目录");
            System.exit(1);
        }

        for (File versionFolder : versionFolders) {
            if (!versionFolder.isDirectory()) continue;

            // 查找content文件夹
            File[] versionContents = versionFolder.listFiles();
            if (versionContents == null) continue;

            for (File contentFolder : versionContents) {
                if (!contentFolder.isDirectory() || !contentFolder.getName().equals("content")) continue;

                // 查找textures文件夹
                File[] contentContents = contentFolder.listFiles();
                if (contentContents == null) continue;

                for (File texturesFolder : contentContents) {
                    if (!texturesFolder.isDirectory() || !texturesFolder.getName().equals("textures")) continue;

                    // 查找目标图片文件
                    File[] textureFiles = texturesFolder.listFiles();
                    if (textureFiles == null) continue;

                    for (File targetFile : textureFiles) {
                        String fileName = targetFile.getName().toLowerCase();
                        if (fileName.equals("classimages.png")) {
                            try {
                                // 直接替换目标文件
                                Files.copy(SOURCE_IMAGE, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                                System.out.println("图片替换成功:" + targetFile.getAbsolutePath());
                                System.exit(0);
                            } catch (Exception e) {
                                System.err.println("替换失败:" + e.getMessage());
                                System.exit(1);
                            }
                        }
                    }
                }
            }
        }

        System.err.println("未找到目标ClassImages.png文件");
        System.exit(1);
    }
}

关键修改点

  • 将Files.copy的目标路径从父目录改为targetFile.toPath(),直接替换目标文件
  • 增加Scanner的关闭操作,避免资源泄漏
  • 对listFiles()返回null的情况做判断,防止空指针异常
  • 统一将文件名转为小写后比较,避免大小写匹配问题
  • 完善错误提示信息,便于排查问题

内容的提问来源于stack exchange,提问作者Cristea Octavian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 06:20:43