java - 加载外部文件作为资源
问题描述
我正在加载资源文件,如下所示
public static JSONArray readConfig(String fileName) throws IOException, JSONException{
ClassPathResource res = new ClassPathResource(fileName);
byte[] bdata = FileCopyUtils.copyToByteArray(res.getInputStream());
String json = new String(bdata, StandardCharsets.UTF_8);
JSONArray jarr = new JSONArray(json);
return jarr;
};
我想知道是否可以将“/something/something1/config.json”之类的外部位置/文件添加到资源/类路径中,以便可以通过与上面相同的代码读取。
谢谢...
解决方案
您可以在类路径中添加文件夹位置。
java -Dloader.path="/something/something1/" -jar your-app.jar
参见文档链接_loader.path
如果要加载不在类路径中的资源,请使用FileSystemResource
而不是,ClassPathResource
这样它将在文件系统中加载资源。
推荐阅读
- r - 根据日期时间和事件创建序列列以获取订单
- web-component - 扩展的 HTML 元素:“是”,包裹一个 HTML 元素,或其他任何东西 - 当前状态是什么?
- google-bigquery - 使用 OVER PARTITION BigQuery 在给定时间之前获取最新记录
- onedrive - 将文件从本地共享复制到共享一个驱动器文件夹
- javascript - 在命名空间存储中调度操作 (Vuex/Nuxt)
- c - C:scanf() 挂起,getchar() 无法清除输入
- json - 如何在 Swift 中映射这个 JSON?
- java - Spring Data JPA 将存储过程结果从多个数据源映射到非实体 POJO
- r - 在本地采购 Rcpp 可以工作,但不能并行化
- r - 为什么我不能在 RStudio 中安装 Plotly 包?错误说明了什么?