spring常用方法

1. 读取配置文件信息

方式一:

java 复制代码
// 获取文件路径
String fileName = "application.yaml";
String filePath = this.getClass().getClassLoader().getResource(fileName).getPath();

BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
Stringline = null;
StringBuilder fileData = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
    fileData.append(line).append(System.lineSeparator());
}
bufferedReader.close();

//或
StringBuilder fileData = new StringBuilder();
int value;
while ((value = bufferedReader.read()) != -1) {
    char c = (char) value;
    fileData.append(c);
}
bufferedReader.close();

//或使用hutool的IoUtil
String fileData = IoUtil.read(new FileInputStream(path), Charset.forName("UTF-8"));
System.out.println(fileData);

方式二:

java 复制代码
String fileName = "application.yaml";
ClassPathResource classPathResource = new ClassPathResource(fileName);
String fileData = IoUtil.read(classPathResource.getStream(), Charset.forName("utf-8"));

方式三:

java 复制代码
String fileData = IoUtil.read(JDBCUtils.class.getClassLoader().getResourceAsStream("application.yaml"), Charset.forName("UTF-8"));
System.out.println(fileData);

方式四:

java 复制代码
String fileData = IoUtil.read(ResourceUtil.getStream("application.yaml"), Charset.forName("UTF-8"));
System.out.println(fileData);
相关推荐
7哥♡ۣۖᝰꫛꫀꪝۣℋ24 分钟前
微服务负载均衡
spring·微服务
Boop_wu2 小时前
Spring生态
java·后端·spring
清水白石0083 小时前
深入解析 LRU 缓存:从 `@lru_cache` 到手动实现的完整指南
java·python·spring·缓存
夕除4 小时前
js--15
java·jvm·spring
sun03224 小时前
【架构基础】Spring中的PropertySourcesPlaceholderConfigurer介绍 (并非新知识,比较古老的一种使用方式)
java·spring·架构
Coder_Boy_4 小时前
Java开发者破局指南:跳出内卷,借AI赋能,搭建系统化知识体系
java·开发语言·人工智能·spring boot·后端·spring
NE_STOP4 小时前
spring6-代理模式和AOP
spring
代码栈上的思考5 小时前
SpringBoot 拦截器
java·spring boot·spring
那我掉的头发算什么6 小时前
【Mybatis】Mybatis-plus使用介绍
服务器·数据库·后端·spring·mybatis