如何从资源文件夹中获取文件?

时间:2020-03-10 07:52:28

标签: java

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

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");

0 个答案:

没有答案