解决Java中properties文件编码问题
作者:KING丨殇痕 时间:2022-07-21 14:09:06
1、properties文件显示乱码问题
原因是因为properties默认使用ASCII码,就算在文件中填写了中文,再打开后依然会转换成ASCII码的形式。
首先确定properties配置文件的编码格式,通常情况下properties的默认编码格式为ISO-8859-1。
更改properties的编码格式为UTF-8:
IDEA:设置->文件编码
eclipse:右键该文件->properties
这里不但设置了编码格式为UTF-8,旁边还有Transparent native-to-ascii conversion选项(eclipse里面没有),这个东西有啥作用呢
2、读取properties文件乱码
设置完properties文件编码格式为UTF-8后,一般我们通过字节流读取properties文件的方式会乱码:
public void TestProp1() throws IOException {
Properties properties = new Properties();
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("application.properties");
properties.load(in);
System.out.println(properties.getProperty("yaml.name"));
}
解决办法就是通过字符流的方式读取properties文件:
public void TestProp() throws IOException {
Properties properties = new Properties();
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("application.properties");
properties.load(new InputStreamReader(in, "UTF-8"));
System.out.println(properties.getProperty("yaml.name"));
}
3、Spring boot的@ConfigurationProperties读取properties文件乱码
方法一
使用yml文件
方法二
设置Transparent native-to-ascii conversion也就是上述图片上属性文件的配置勾选自动转换成ASCII,但显示原生的内容。
在IDEA勾选这个选项的作用就是:显示为UTF-8格式,但是运行时转换成ASCII的形式,实际上使用的是native2ascii.exe来进行转换。
运行时显示如下图:
方法三
添加注解@PropertySource并声明encoding=“UTF-8”
//加注解
@Component
@ConfigurationProperties(prefix = "yaml")
@PropertySource(value = {"classpath:yaml.properties"}, encoding = "UTF-8")
注意:这种方法只能对自定义的properties文件有效,对于spring boot默认生成的application.properties没有效果
来源:https://blog.csdn.net/sunp_csdn/article/details/122821197
标签:Java,properties
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java集合Collection实现类解析ArrayList LinkedList及Vector
2022-05-01 05:44:12
![](https://img.aspxhome.com/file/2023/6/61956_0s.png)
Java日常练习题,每天进步一点点(7)
2023-11-27 00:34:58
![](https://img.aspxhome.com/file/2023/8/59978_0s.png)
Android Flutter实现搜索的三种方式详解
2023-07-10 18:00:49
java~springboot~ibatis数组in查询的实现方法
2023-03-31 21:31:13
java生成验证码图片的方法
2023-12-09 08:57:15
详解Java的引用类型及使用场景
2021-06-27 11:34:18
一文看懂JAVA设计模式之工厂模式
2023-11-27 02:30:54
![](https://img.aspxhome.com/file/2023/8/60338_0s.png)
Java内存模型JMM详解
2023-11-28 13:08:50
![](https://img.aspxhome.com/file/2023/4/59784_0s.png)
Tomcat 实现WebSocket详细介绍
2023-08-12 21:22:11
![](https://img.aspxhome.com/file/2023/0/58150_0s.png)
浅谈基于SpringBoot实现一个简单的权限控制注解
2022-01-23 16:42:47
你所不知道的Spring自动注入详解
2021-09-04 19:30:08
![](https://img.aspxhome.com/file/2023/8/60828_0s.png)
Java并发编程预防死锁过程详解
2023-11-09 15:33:58
合并有序数组的实现(java与C语言)
2023-08-16 13:45:40
Java实现动态获取文件的绝对路径
2022-12-19 00:34:14
![](https://img.aspxhome.com/file/2023/2/61392_0s.png)
Java守护线程实例详解_动力节点Java学院整理
2023-03-29 08:14:30
![](https://img.aspxhome.com/file/2023/5/60175_0s.png)
C语言时间函数之strftime()详解
2023-06-26 02:42:32
![](https://img.aspxhome.com/file/2023/8/103098_0s.png)
Java全面分析面向对象之继承
2023-11-23 11:55:59
![](https://img.aspxhome.com/file/2023/5/59735_0s.png)
带你详细了解Java值传递和引用传递
2023-02-19 08:42:26
![](https://img.aspxhome.com/file/2023/9/61009_0s.jpg)
IDEA 2019.1.3 激活码大全
2023-05-09 05:20:37
IDEA+Maven搭建Spring环境的详细教程
2023-11-25 07:50:34
![](https://img.aspxhome.com/file/2023/0/59880_0s.png)