简体   繁体   English

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

[英]Reading from property file containing utf 8 character

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. output 的格式不正确。 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上述消息的output为

在此处输入图像描述

You can see the message in the console after the line您可以在该行之后的控制台中看到消息

{************************ 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.我可以用另一种方法读取这个文件,但我正在寻找更少的代码修改。

Use an InputStreamReader with 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")));

As a method, this may resemble the following:作为一种方法,这可能类似于以下内容:

  private Properties read( final Path file ) throws IOException {
    final var properties = new Properties();

    try( final var in = new InputStreamReader(
      new FileInputStream( file.toFile() ), StandardCharsets.UTF_8 ) ) {
      properties.load( in );
    }

    return properties;
  }

Don't forget to close your streams.不要忘记关闭你的流。 Java 7 introduced StandardCharsets.UTF_8 . Java 7 引入了StandardCharsets.UTF_8

Use props.load(new FileReader("uinsoaptest.properties")) instead.使用props.load(new FileReader("uinsoaptest.properties"))代替。 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 .默认情况下,它使用编码Charset.forName(System.getProperty("file.encoding"))可以使用System.setProperty("file.encoding", "UTF-8")或命令行设置为 UTF-8参数-Dfile.encoding=UTF-8

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"));
}

You should specify the UTF-8 encoding when you construct your FileInputStream object.您应该在构造 FileInputStream 对象时指定 UTF-8 编码。 You can use this constructor:您可以使用此构造函数:

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

If anybody comes across this problem in Kotlin , like me: The accepted solution of @Würgspaß works here as well.如果有人在Kotlin中遇到这个问题,就像我一样:@Würgspaß 的公认解决方案也适用于此。 The corresponding Kotlin syntax:对应的 Kotlin 语法:

Instead of the usual而不是通常的

val properties = Properties()
filePath.toFile().inputStream().use { stream -> properties.load(stream) }

I had to use我不得不使用

val properties = Properties()
InputStreamReader(FileInputStream(filePath.toFile()), StandardCharsets.UTF_8).use { stream -> properties.load(stream) }

With this, special UTF-8 characters are loaded correctly from the properties file given in filePath .这样,特殊的 UTF-8 字符就会从filePath中给出的属性文件中正确加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM