手动读取配置文件乱码
时间: 2023-08-14 21:07:11 浏览: 186
Ubuntu解压zip文件乱码的解决方法
5星 · 资源好评率100%
如果你在手动读取配置文件时遇到了乱码问题,可以通过指定编码格式来解决。你可以使用以下方法来读取配置文件并手动设置为指定编码来读取:
```java
public static String readPropertiesFile(String key) throws FileNotFoundException, IOException {
try {
Resource fileResource = new ClassPathResource("application.properties");
Properties props = new Properties();
props.load(new InputStreamReader(fileResource.getInputStream(), "指定编码格式"));
String value = props.getProperty(key);
System.out.println(key + "键的值是:" + value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
```
在上述代码中,你需要将"指定编码格式"替换为你想要使用的编码格式,比如"UTF-8"。通过将编码格式指定为正确的值,你就可以正确读取配置文件中的中文内容,避免乱码问题的发生。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* [SpringBoot 读取配置文件的 5 种方法!](https://blog.csdn.net/m0_71777195/article/details/126419460)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
- *2* [解决spring boot 读取配置文件的中文乱码异常](https://blog.csdn.net/qq_40587263/article/details/115294585)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
- *3* [解决读取properties配置文件中文乱码问题](https://blog.csdn.net/aixinaxc/article/details/105139523)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
[ .reference_list ]
阅读全文