加载资源文件的几种方式

作用:加载资源文件
注意:classpath:后面接的是文件地址 当前线程的resource下的资源文件才可以接受得到。

File jsonFile = null;
try {
jsonFile = ResourceUtils.getFile("classpath:");
String contentsAsString = FileUtils.getContentsAsString(jsonFile);
stuBaseInfoJson = JSONObject.parseObject(contentsAsString);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

作用:加载资源文件
注意:直接加文件地址就可以 资源文件首先找的是父类的地址 其次 找的是虚拟机中类加载器中的配置。

InputStream resource = VelocityUtils.class.getClassLoader().getResourceAsStream("");
BufferedReader br = new BufferedReader(new InputStreamReader(resource));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
原文地址:https://www.cnblogs.com/miaoww/p/8405858.html