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");
解决方案
推荐阅读
- python - 如何重塑 np 3d 数组中的帧数据?
- python - 缺少主题引擎?
- ios - Firebase - 从常量设置字典中的键时,updateChildValues 使应用程序崩溃
- postgresql - postgresql中的总时间和执行时间之间的差异?
- django - 我们如何在 saleor django 框架中实现 google 登录注册?
- r - 根据条件 (R) 收集变量
- javascript - 如何获取父div的边距顶部?
- css - 如何使下拉菜单正确显示在具有水平滚动条的容器之外
- c - 使用 C 中的函数指针将系统调用传递给函数
- macos - gdb 8.2 在 macOS Mojave 10.14 上无法识别可执行文件