How to use UTF-8 in resource properties with ResourceBundle

后端 未结 16 2140
难免孤独
难免孤独 2020-11-22 03:28

I need to use UTF-8 in my resource properties using Java\'s ResourceBundle. When I enter the text directly into the properties file, it displays as mojibake.

相关标签:
16条回答
  • 2020-11-22 03:36

    Given that you have an instance of ResourceBundle and you can get String by:

    String val = bundle.getString(key); 
    

    I solved my Japanese display problem by:

    return new String(val.getBytes("ISO-8859-1"), "UTF-8");
    
    0 讨论(0)
  • 2020-11-22 03:36

    From Java 9, the default to load properties file has been changed to UTF-8. https://docs.oracle.com/javase/9/intl/internationalization-enhancements-jdk-9.htm

    0 讨论(0)
  • 2020-11-22 03:37
    package com.varaneckas.utils;  
    
    import java.io.UnsupportedEncodingException;  
    import java.util.Enumeration;  
    import java.util.PropertyResourceBundle;  
    import java.util.ResourceBundle;  
    
    /** 
     * UTF-8 friendly ResourceBundle support 
     *  
     * Utility that allows having multi-byte characters inside java .property files. 
     * It removes the need for Sun's native2ascii application, you can simply have 
     * UTF-8 encoded editable .property files. 
     *  
     * Use:  
     * ResourceBundle bundle = Utf8ResourceBundle.getBundle("bundle_name"); 
     *  
     * @author Tomas Varaneckas <tomas.varaneckas@gmail.com> 
     */  
    public abstract class Utf8ResourceBundle {  
    
        /** 
         * Gets the unicode friendly resource bundle 
         *  
         * @param baseName 
         * @see ResourceBundle#getBundle(String) 
         * @return Unicode friendly resource bundle 
         */  
        public static final ResourceBundle getBundle(final String baseName) {  
            return createUtf8PropertyResourceBundle(  
                    ResourceBundle.getBundle(baseName));  
        }  
    
        /** 
         * Creates unicode friendly {@link PropertyResourceBundle} if possible. 
         *  
         * @param bundle  
         * @return Unicode friendly property resource bundle 
         */  
        private static ResourceBundle createUtf8PropertyResourceBundle(  
                final ResourceBundle bundle) {  
            if (!(bundle instanceof PropertyResourceBundle)) {  
                return bundle;  
            }  
            return new Utf8PropertyResourceBundle((PropertyResourceBundle) bundle);  
        }  
    
        /** 
         * Resource Bundle that does the hard work 
         */  
        private static class Utf8PropertyResourceBundle extends ResourceBundle {  
    
            /** 
             * Bundle with unicode data 
             */  
            private final PropertyResourceBundle bundle;  
    
            /** 
             * Initializing constructor 
             *  
             * @param bundle 
             */  
            private Utf8PropertyResourceBundle(final PropertyResourceBundle bundle) {  
                this.bundle = bundle;  
            }  
    
            @Override  
            @SuppressWarnings("unchecked")  
            public Enumeration getKeys() {  
                return bundle.getKeys();  
            }  
    
            @Override  
            protected Object handleGetObject(final String key) {  
                final String value = bundle.getString(key);  
                if (value == null)  
                    return null;  
                try {  
                    return new String(value.getBytes("ISO-8859-1"), "UTF-8");  
                } catch (final UnsupportedEncodingException e) {  
                    throw new RuntimeException("Encoding not supported", e);  
                }  
            }  
        }  
    }  
    
    0 讨论(0)
  • 2020-11-22 03:40

    ResourceBundle.Control with UTF-8 and new String methods don't work, if the properties file uses cp1251 charset, for example.

    So I recomended using a common method: write in unicode symbols. For this:

    IDEA -- has a special "Transparent native-to-ASCII conversion" option (Settings > File Encoding).

    Eclipse -- has a plugin "Properties Editor". It can work as separate application.

    0 讨论(0)
  • 2020-11-22 03:40

    http://sourceforge.net/projects/eclipse-rbe/

    as already stated property files should be encoded in ISO 8859-1

    You can use the above plugin for eclipse IDE to make the Unicode conversion for you.

    0 讨论(0)
  • 2020-11-22 03:42

    look at this : http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)

    the properties accept an Reader object as arguments, which you can create from an InputStream.

    at the create time, you can specify the encoding of the Reader:

    InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
    

    then apply this Reader to the load method :

    prop.load(isr);
    

    BTW: get the stream from .properties file :

     InputStream stream = this.class.getClassLoader().getResourceAsStream("a.properties");
    

    BTW: get resource bundle from InputStreamReader:

    ResourceBundle rb = new PropertyResourceBundle(isr);
    

    hope this can help you !

    0 讨论(0)
提交回复
热议问题