01 引言
读取配置文件内容的姿势,从一定程度上反映了编程的基本功法,配置文件的读取方式也就是你薪资的刻度尺。快来看看你属于哪个段位?
02 白银「8-15K」
通过properties
文件的标准读取方式获取资源信息,无论是工具类还是其他都是使用流的方式读取配置文件。侧重点在单独的读取配置文件,一个main
或者test
方法就可以实现。
数据准备:offer.level=白银
2.1 手撕配置文件的读取方法
FileInputStream fileInputStream = new FileInputStream("src\\main\\resources\\application.properties");
// 解决中文乱码问题
InputStreamReader reader = new InputStreamReader(fileInputStream, "utf-8");
Properties properties = new Properties();
properties.load(reader);
System.out.println("properties 读取:" + properties.getProperty("offer.level"));
2.2 使用ClassLoader
InputStream is = ClassLoader