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);
相关推荐
听忆.1 小时前
RabbitMQ消息可靠性等机制详解(精细版三)
java·开发语言·spring boot·后端·spring·java-ee·rabbitmq
Rainbow_19912 小时前
SpringCloud_Eureka注册中心
spring·spring cloud·eureka
kussmcx3 小时前
开始尝试从0写一个项目--后端(一)
java·spring·maven
yogima3 小时前
在Spring Data JPA中使用@Query注解
java·数据库·spring
时间瑾4 小时前
SpringMVC的视图
spring·springmvc
高级程序源4 小时前
springboot学生档案信息管理系统-计算机毕业设计源码96509
java·spring boot·spring·eclipse·mybatis·idea
jupiter_8885 小时前
spring tx @Transactional 详解 `Advisor`、`Target`、`ProxyFactory
spring·事务·aop
zhangkai__7 小时前
SpringCloud Feign 报错 Request method ‘POST‘ not supported 的解决办法
python·spring·spring cloud
hummhumm8 小时前
数据结构第08小节:双端队列
java·数据结构·spring boot·spring·java-ee·maven·intellij-idea
firepation8 小时前
使用 Spring 配置数据库连接池:HikariCP 与 Apache DBCP
spring