首页 > 解决方案 > 如何从资源文件夹中获取文件?

问题描述

我试图从资源文件夹加载文件,但它抛出了异常:

java.io.FileNotFoundException: C:\PROJECTS%20FOR%20FOXMINDED\7%20TASK%20-%20SQL\target\classes\init.sql (The system cannot find the path specified)

我试图从资源中获取文件的类:

public class FileLoader {

public File getFileFromResources(String fileName) {


            ClassLoader classLoader = getClass().getClassLoader();

    URL resource = classLoader.getResource(fileName);
    if (resource == null) {
        throw new IllegalArgumentException("file is not found!");
    } else {
        return new File(resource.getFile());
    }
}

}

在 Main.class 我试过这样:File initializationDbSqlFile = fileLoader.getFileFromResources("init.sql");

标签: java

解决方案


推荐阅读