Home  >  Article  >  Java  >  Java Error: Java11 UTF-8 Properties File Error, How to Handle and Avoid

Java Error: Java11 UTF-8 Properties File Error, How to Handle and Avoid

WBOY
WBOYOriginal
2023-06-25 09:26:271584browse

Java is a cross-platform programming language that is widely used in the development of web applications, mobile applications, desktop applications and other fields. When Java developers write code, they often need to use property files to store configuration information, internationalized strings and other data. However, applications developed using Java 11 may encounter UTF-8 properties file errors. This article will explain how to handle and avoid Java11 UTF-8 properties file errors.

1. Symptoms of UTF-8 attribute file errors

In Java11, if UTF-8 encoding is used to save the attribute file, and the attribute file contains Chinese, Japanese and other non-ASCII characters, it will You may encounter the following exception:

java.nio.charset.MalformedInputException: Input length = 1
at java.base/java.nio.charset.CoderResult.throwException(CoderResult.java:274)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185)
at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)

It is difficult to see the root cause of the problem with this exception message, but through analysis, the following conclusions can be drawn:

  • When reading the properties file, Java11 uses UTF-8 encoding by default.
  • In the process of reading the properties file, Java will read it byte by byte according to UTF-8 encoding rules.
  • If a byte sequence containing non-ASCII characters is read, Java will try to convert it to Unicode characters.
  • If the read byte sequence cannot be decoded into Unicode characters according to UTF-8 encoding rules, a MalformedInputException exception will be thrown.

2. How to avoid UTF-8 property file errors

  1. Modify the property file encoding

If you use UTF-8 encoding to save the property file When the above exception occurs, you can try to change the encoding of the attribute file to ISO-8859-1 or GB2312 or other encoding formats. These two encoding formats do not support Unicode characters, so MalformedInputException will not occur. However, it should be noted that modifying the encoding format may cause non-ASCII characters to be lost or garbled.

  1. Modify the code that reads the properties file

Modify the code that reads the properties file to read the properties file using the correct character set. In Java11, when using the Properties class to read property files, ISO-8859-1 encoding is used by default, so garbled characters may appear when reading non-ASCII characters such as Chinese. You can read the property file in UTF-8 format through the following code:

InputStream inputStream = this.getClass().getResourceAsStream("/test.properties");
Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
Properties properties = new Properties();
properties.load(reader);

This method will set the encoding format of the property file to UTF-8 to ensure that the property file containing Chinese and other non-ASCII characters can be read correctly. properties file.

3. How to deal with UTF-8 attribute file errors

If you have encountered a UTF-8 attribute file error, you can solve it through the following steps:

  1. Confirm Whether the properties file contains non-ASCII characters

Use any text editor to open the properties file and check whether the file content contains non-ASCII characters such as Chinese, Japanese, Korean, etc. If the properties file does not contain these characters, then you need to check for other problems in the code.

  1. Confirm the encoding format of the properties file

Use a text editor to open the properties file and check the encoding format of the file. If it is in UTF-8 encoding format and the file contains non-ASCII characters, a MalformedInputException may occur.

  1. Modify the property file encoding format or read the code of the property file

According to the specific situation, you can choose to modify the property file encoding format or read the code of the property file to Avoid or resolve MalformedInputException exceptions.

In short, when using property files in UTF-8 encoding format in Java11, you need to pay special attention to the issue of character set encoding. Correctly setting the encoding format and the code for reading the properties file can effectively avoid the problem of UTF-8 property file errors.

The above is the detailed content of Java Error: Java11 UTF-8 Properties File Error, How to Handle and Avoid. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn