在springboot项目中,想要读取自定义的配置文件,但是读取到以后是乱码,如何解决。
项目目录
配置文件内容
具体读取代码
public class ResourceUtil {
public static String readResourceConfig(String configFileName, String key) throws IOException {
// 加载资源
Resource resource = new ClassPathResource(configFileName,ClassLoader.getSystemClassLoader());
// 定义InputStreamReader加载输入流 并且指定编码
InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream(), "GBK");
// 创建Properties
Properties properties = new Properties();
// 加载输入流
properties.load(inputStreamReader);
// 关闭流
inputStreamReader.close();
// 加载properties的key
return properties.getProperty(key);
}
// 测试
public static void main(String[] args) throws IOException {
String ywqqlsh = readResourceConfig("config.properties", "ywqqlsh");
System.out.println(ywqqlsh);
}
}