java - 如何在java中导出resources文件夹中的所有文件和文件夹

标签 java embedded-resource

在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);
    }
}

关于java - 如何在java中导出resources文件夹中的所有文件和文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40569537/

相关文章:

java - JTabbedPane 的布局设置

asp.net-mvc - 如何利用MVC中的常用图像资源

asp.net-mvc-3 - 使用自定义VirtualPathProvider加载嵌入资源部分 View

c# - 读取嵌入的文本文件

java - 如何在一维数组Java中上下左右搜索

java - Android/java - 更改 Runnable 类中的按钮可见性

java - GAE 中的持久 http session (浏览器/实例重新启动)- Java

java - Java 8 中的组合谓词

ios - 获取导航栏的默认返回指示器图像

c# - 从嵌入式资源加载模板