Java: How to write “Arabic” in properties file?

前端 未结 7 1984
自闭症患者
自闭症患者 2021-01-05 09:32

I want to write \"Arabic\" in the message resource bundle (properties) file but when I try to save it I get this error:

\"Save couldn\'t be completed Some characters

7条回答
  •  广开言路
    2021-01-05 09:57

    Properties-based resource bundles must be encoded in ISO-8859-1 to use the default loading mechanism, but I have successfully used this code to allow the properties files to be encoded in UTF-8:

    private static class ResourceControl extends ResourceBundle.Control {
        @Override
        public ResourceBundle newBundle(String baseName, Locale locale,
                String format, ClassLoader loader, boolean reload)
                throws IllegalAccessException, InstantiationException,
                IOException {
            String bundlename = toBundleName(baseName, locale);
            String resName = toResourceName(bundlename, "properties");
            InputStream stream = loader.getResourceAsStream(resName);
            return new PropertyResourceBundle(new InputStreamReader(stream,
                    "UTF-8"));
        }
    
    }
    

    Then of course you have to change the encoding of the file itself to UTF-8 in your IDE, and can use it like this:

    ResourceBundle bundle = ResourceBundle.getBundle(
        "package.Bundle", new ResourceControl());
    

提交回复
热议问题