How to use UTF-8 in resource properties with ResourceBundle

后端 未结 16 2219
难免孤独
难免孤独 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:54

    Open the Settings / Preferences dialog (Ctrl + Alt + S), then click Editor and File Encodings.

    Screenshot of window shown

    Then, on the bottom, you will fing default encodings for properties files. Choose your encoding type.

    Alternatively you can use unicode symbols instead of text in your resource bundle (for example "ів" equals \u0456\u0432)

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

    Here's a Java 7 solution that uses Guava's excellent support library and the try-with-resources construct. It reads and writes properties files using UTF-8 for the simplest overall experience.

    To read a properties file as UTF-8:

    File file =  new File("/path/to/example.properties");
    
    // Create an empty set of properties
    Properties properties = new Properties();
    
    if (file.exists()) {
    
      // Use a UTF-8 reader from Guava
      try (Reader reader = Files.newReader(file, Charsets.UTF_8)) {
        properties.load(reader);
      } catch (IOException e) {
        // Do something
      }
    }
    

    To write a properties file as UTF-8:

    File file =  new File("/path/to/example.properties");
    
    // Use a UTF-8 writer from Guava
    try (Writer writer = Files.newWriter(file, Charsets.UTF_8)) {
      properties.store(writer, "Your title here");
      writer.flush();
    } catch (IOException e) {
      // Do something
    }
    
    0 讨论(0)
  • 2020-11-22 03:57

    Attention: java property files should be encoded in ISO 8859-1!

    ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence.

    @see Properties Java Doc

    If you still really want to do this: have a look at: Java properties UTF-8 encoding in Eclipse -- there are some code samples

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

    I tried to use the approach provided by Rod, but taking into consideration BalusC concern about not repeating the same work-around in all the application and came with this class:

    import java.io.UnsupportedEncodingException;
    import java.util.Locale;
    import java.util.ResourceBundle;
    
    public class MyResourceBundle {
    
        // feature variables
        private ResourceBundle bundle;
        private String fileEncoding;
    
        public MyResourceBundle(Locale locale, String fileEncoding){
            this.bundle = ResourceBundle.getBundle("com.app.Bundle", locale);
            this.fileEncoding = fileEncoding;
        }
    
        public MyResourceBundle(Locale locale){
            this(locale, "UTF-8");
        }
    
        public String getString(String key){
            String value = bundle.getString(key); 
            try {
                return new String(value.getBytes("ISO-8859-1"), fileEncoding);
            } catch (UnsupportedEncodingException e) {
                return value;
            }
        }
    }
    

    The way to use this would be very similar than the regular ResourceBundle usage:

    private MyResourceBundle labels = new MyResourceBundle("es", "UTF-8");
    String label = labels.getString(key)
    

    Or you can use the alternate constructor which uses UTF-8 by default:

    private MyResourceBundle labels = new MyResourceBundle("es");
    
    0 讨论(0)
提交回复
热议问题