Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of properties File Encoding in Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is a detailed introduction to "how to solve the problem of properties file coding in Java". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to solve the problem of properties file coding in Java" can help you solve your doubts. The following is a small series of ideas to slowly deepen and learn new knowledge together.

1. The properties file shows garbled code problems

The reason is that properties uses ASCII code by default. Even if Chinese is filled in the file, it will still be converted to ASCII code after opening it.

First determine the encoding format of the properties configuration file, usually the default encoding format of properties is ISO-8859-1.

Change the encoding format of properties to UTF-8:

IDEA: Settings-> File encoding

eclipse: right-click on the file->properties

Not only is the encoding format set to UTF-8, but there is also the Transparent native-to-ascension conversion option next to it (not in eclipse). What does this thing do?

2. Read the properties file garbled

After setting the encoding format of the properties file to UTF-8, the way we usually read the properties file through byte stream will be garbled:

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

The solution is to read the properties file as a stream of characters:

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.@ConfigurationProperties of Spring boot reads properties file garbled

method one

Using yml files

methodology II

Set Transparent native-to-ascension conversion, i.e. the configuration check on the property file above automatically converts to ASCII, but displays the native content.

Checking this option in IDEA causes it to display in UTF-8 format, but convert to ASCII at runtime, actually using native2ascii.exe for conversion.

The runtime display is as follows:

method three

Add annotation @PropertySource and declare encoding="UTF-8"

//annotation @Component@ConfigurationProperties(prefix = "yaml")@PropertySource(value = {"classpath: yaml.properties"}, encoding = "UTF-8")

Note: This method only works with custom properties files, and has no effect on application.properties generated by spring boot by default

Read here, this article "Java properties file coding problem how to solve" article has been introduced, want to master the knowledge of this article also need to practice to understand, if you want to know more about the content of the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report