下面这段代码读取properties文件时汉字乱码
Properties kuraDefaults = new Properties();
URL kuraConfigUrl = new URL("path")
try (InputStream in = kuraConfigUrl.openStream()) {
kuraDefaults.load(in);
}
使用InputStreamReader
来指定UTF-8编码,确保正确读取汉字
Properties kuraDefaults = new Properties();
URL kuraConfigUrl = new URL(kuraConfig);
try (InputStream in = kuraConfigUrl.openStream();
InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
kuraDefaults.load(reader);
}
关键点:
-
InputStreamReader
:用于将字节流转换为字符流,并指定字符编码。 -
StandardCharsets.UTF_8
:指定使用UTF-8编码读取文件。
注意事项:
-
确保Properties文件本身是以UTF-8编码保存的。
-
如果文件编码不是UTF-8,需要根据实际编码调整
StandardCharsets
的值。
通过这种方式,可以避免汉字乱码问题。