Java 从包含 utf 8 字符的属性文件中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30755014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:07:04  来源:igfitidea点击:

Reading from property file containing utf 8 character

javaparsingencodingutf-8

提问by Som

I am reading a property file which consists of a message in the UTF-8 character set.

我正在读取一个属性文件,该文件由 UTF-8 字符集中的消息组成。

Problem

问题

The output is not in the appropriate format. I am using an InputStream.

输出格式不正确。我正在使用一个InputStream.

The property file looks like

属性文件看起来像

username=LBSUSER
password=Lbs@123
url=http://localhost:1010/soapfe/services/MessagingWS
timeout=20000
message=Spanish character are = {á é í, ó,ú ,ü, ?, ?, ?, á, é, í, ó, ú, ü, ?, ?, ?, °, 4° a?o = cuarto a?o, , ¢, £, ¥}

And I am reading the file like this,

我正在阅读这样的文件,

Properties props = new Properties();
props.load(new FileInputStream("uinsoaptest.properties"));
String username = props.getProperty("username", "test");
String password = props.getProperty("password", "12345");
String url = props.getProperty("url", "12345");
int timeout = Integer.parseInt(props.getProperty("timeout", "8000"));
String messagetext = props.getProperty("message");
System.out.println("This is soap msg : " + messagetext);

The output of the above message is

上述消息的输出是

enter image description here

在此处输入图片说明

You can see the message in the console after the line

您可以在行后在控制台中看到消息

{************************ SOAP MESSAGE TEST***********************}

{************************ SOAP MESSAGE TEST***********************}

I will be obliged if I can get any help reading this file properly. I can read this file with another approach but I am looking for less code modification.

如果我能在正确阅读此文件时获得任何帮助,我将不胜感激。我可以用另一种方法读取这个文件,但我正在寻找更少的代码修改。

采纳答案by Würgspa?

Use an InputStreamReaderwith Properties.load(Reader reader):

使用InputStreamReaderProperties.load(Reader reader)

FileInputStream input = new FileInputStream(new File("uinsoaptest.properties"));
props.load(new InputStreamReader(input, Charset.forName("UTF-8")));

回答by Binkan Salaryman

Use props.load(new FileReader("uinsoaptest.properties"))instead. By default it uses the encoding Charset.forName(System.getProperty("file.encoding"))which can be set to UTF-8 with System.setProperty("file.encoding", "UTF-8")or with the commandline parameter -Dfile.encoding=UTF-8.

使用props.load(new FileReader("uinsoaptest.properties"))来代替。默认情况下,它使用Charset.forName(System.getProperty("file.encoding"))可以通过System.setProperty("file.encoding", "UTF-8")命令行参数设置为 UTF-8的编码-Dfile.encoding=UTF-8

回答by Lexicographical

You should specify the UTF-8 encoding when you construct your FileInputStream object. You can use this constructor:

您应该在构造 FileInputStream 对象时指定 UTF-8 编码。您可以使用此构造函数:

new FileInputStream("uinsoaptest.properties", "UTF-8");

If you want to make a change to your JVM so as to be able to read UTF-8 files by default, you will have to change the JAVA_TOOL_OPTIONS in your JVM options to something like this :

如果您想对 JVM 进行更改以便能够默认读取 UTF-8 文件,则必须将 JVM 选项中的 JAVA_TOOL_OPTIONS 更改为如下所示:

-Dfile.encoding=UTF-8

回答by Ilya Khudyakov

If somebody use @Value annotation, could try StringUils.

如果有人使用 @Value 注释,可以尝试 StringUils。

@Value("${title}")
private String pageTitle;

public String getPageTitle() {
    return StringUtils.toEncodedString(pageTitle.getBytes(Charset.forName("ISO-8859-1")), Charset.forName("UTF-8"));
}