garbled characters when retrieving application.properties value in Eclipse

Asked 2 years ago, Updated 2 years ago, 72 views

When I changed the application.properties file to UTF-8 in Eclipse,
If you get it below, the characters will be garbled.
Is there a way to get it without garbled characters?

ResourceBundle=ResourceBundle.getBundle("application");
String test = bundle.getString("test");

★Application.properties is as follows

test=test.

I tried this, but it didn't work...

ResourceBundle=ResourceBundle.getBundle("application");
String test = null;
test = bundle.getString("test");
test = new String(test.getBytes("ISO-8859-1"), "JISAutoDetect";

Also, the Eclipse setting is
US>Window->General->In Content Type
Default java property file encoding is UTF-8.

eclipse

2022-09-29 21:38

1 Answers

I think it is possible to do so as described on the above site.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class ResourceBundleUtf8Control extensionsResourceBundle.Control{

    @ Override
    public ResourceBundle newBundle(final String baseName, final Locale locale, final String format,
        final ClassLoader loader, final boolean reload)
        flowsIllegalAccessException, InstantiationException, IOException {
        final String bundleName = to BundleName(baseName, locale);
        final String resourceName=toResourceName(bundleName, "properties");

        try(InputStream is=loader.getResourceAsStream(resourceName);
            InputStreamReader isr=new InputStreamReader(is, "UTF-8");
            BufferedReader reader = new BufferedReader(isr)) {
            return new PropertyResourceBundle (reader);
        }
    }
}

Define a class similar to the one above, and use it in the following ways:

ResourceBundle=ResourceBundle.getBundle("application", newResourceBundleUtf8Control());

In addition, since Java9 has the default encoding of ResourceBundle to UTF-8, you don't need to use the above method.You can retrieve the code exactly as expected in the questionnaire.

However, I was concerned that the file name was application.properties.Is that really a resource bundle?

For example, if it is the Spring Boot configuration file, it should be treated with ISO 8859-1 instead of converting Java 9 or later to UTF-8 because it is not a resource bundle but just a property file.

Properties#load(InputStream):

The input stream is a simple line-oriented format specified by load (Reader) and is assumed to use ISO 8859-1 character encoding.In other words, each byte has one Latin character. Characters that are not present in Latin 1 and some special characters are represented using Unicode escapees defined in Section 3.3 of Java™ Language Specifications within keys and elements.

In fact, Spring Boot treats application.properties as such:

If you want the ISO 8859-1 encoded property file to be displayed on Eclipse as expected (so that you can read Japanese), you may be able to use the following plug-in:


2022-09-29 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.