如何导出 java 资源文件夹中的所有文件和文件夹

How to export all files and folders in resources folder in java

如何在 java 代码中将 jar 文件内的 src/main/resources 目录中的所有内容复制到 jar 文件的同一目录中?

这是我从 org.bukkit.plugin.java.JavaPlugin 中找到的,它似乎有效

public void saveResource(String resourcePath, boolean replace) {
    if (resourcePath == null || resourcePath.equals("")) {
        throw new IllegalArgumentException("ResourcePath cannot be null or empty");
    }

    resourcePath = resourcePath.replace('\', '/');
    InputStream in = getResource(resourcePath);
    if (in == null) {
        throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found");
    }

    File outFile = new File(dataFolder, resourcePath);
    int lastIndex = resourcePath.lastIndexOf('/');
    File outDir = new File(dataFolder, resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0));

    if (!outDir.exists()) {
        outDir.mkdirs();
    }

    try {
        if (!outFile.exists() || replace) {
            OutputStream out = new FileOutputStream(outFile);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.close();
            in.close();
        } else {
            logger.log(Level.WARNING, "Could not save " + outFile.getName() + " to " + outFile + " because "
                    + outFile.getName() + " already exists.");
        }
    } catch (IOException ex) {
        logger.log(Level.SEVERE, "Could not save " + outFile.getName() + " to " + outFile, ex);
    }
}