Transfer: Solve the problem of Chinese garbled characters in .properties files in Eclipse

When I wrote comments in the .properties file, I found that the Chinese characters were garbled. Since I saw the encoding type of the .properties file in the idea before, I found the reason for the garbled characters.

In the Chinese operating system, the default encoding of Java type files in Eclipse is GBK, but the default encoding of Properties resource files is ISO-8859-1. So editing the Chinese in the Java file will not have problems, but editing the Chinese in the Properties resource file will have problems.

Solve the garbled method 1:

Right-click the properties file->properties->Resource->Text file encoding, select other, select other encoding methods, such as UTF-8, so that you can enter Chinese in the properties without automatically converting to Unicode.

In addition, in fact, if the key value is in Chinese, it is not necessarily unreadable. For example, in the following cases, the correct encoding method is selected when opening the file stream, so that the Chinese can be loaded by the load method of properties. (But in some cases, if you can't choose the encoding method, there will be problems naturally)

BufferedReader br = null;
Properties datas = new Properties();
br = new BufferedReader(new InputStreamReader(new  FileInputStream(new File("data.properties")), "GBK"));
datas.load(br);

Although the above method is solved, but I want to set it every time I want to use it in the future, so I want to solve it once, and set it up in the future.

Solve the garbled method 2

Directly modify the default encoding of the Properties resource file. In the main interface of Eclipse, open Window->Perferences->General->ContentTypes: 
 
find the Java Properties File type file under the Text type. After selecting it, there will be a default character encoding below, manually Enter UTF-8, then update, then ok

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324930891&siteId=291194637