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);
相关推荐
f***a3461 小时前
开源模型应用落地-工具使用篇-Spring AI-高阶用法(九)
人工智能·spring·开源
大云计算机毕设5 小时前
【2026计算机毕设选题】计算机毕设全新推荐项目选题指南(70+精选热门方向)
spring·数据分析·毕业设计·课程设计·毕设
空空kkk6 小时前
SpringMVC——拦截器
java·数据库·spring·拦截器
梵得儿SHI6 小时前
(第七篇)Spring AI 基础入门总结:四层技术栈全景图 + 三大坑根治方案 + RAG 进阶预告
java·人工智能·spring·springai的四大核心能力·向量维度·prompt模板化·向量存储检索
q***42827 小时前
SpringCloud-持久层框架MyBatis Plus的使用与原理详解
spring·spring cloud·mybatis
KotlinKUG贵州7 小时前
SpringGateway-MVC对SSE转发出现阻塞响应问题的分析和解决
spring·spring cloud·kotlin
l***77527 小时前
总结:Spring Boot 之spring.factories
java·spring boot·spring
x***01069 小时前
springboot中配置logback-spring.xml
spring boot·spring·logback
q***25110 小时前
Spring容器的开启与关闭
java·后端·spring
0***m82210 小时前
Maven Spring框架依赖包
java·spring·maven