I don't want to use the native2ascii command, but I also don't want to unicode escape ... A note when dealing with UTF-8 encoded property files.
If you handle the property file containing multi-byte characters as it is, the characters will be garbled. Prepare a subclass that inherits the ResourceBundle.Control class. So, I overridden the newBundle () method in the prepared subclass and specified UTF-8 encoding. Implement to handle InputStreamReader object.
ResourceBundleWithEncording.java
/**
* UTF-8 Generate a ResourceBundle object from the encoded properties file
*/
public class ResourceBundleWithEncording extends ResourceBundle.Control {
private static final String SUFFIX = "properties";
private static final String ENCODE = "UTF-8";
@Override
public ResourceBundle newBundle(String baseName, Locale locale,
String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, SUFFIX);
try (InputStream is = loader.getResourceAsStream(resourceName);
InputStreamReader isr = new InputStreamReader(is, ENCODE);
BufferedReader reader = new BufferedReader(isr)) {
return new PropertyResourceBundle(reader);
}
}
}
After that, just specify the created subclass object in the argument of the ResourceBundle.getBundle method.
hoge.java
ResourceBundle rb = ResourceBundle.getBundle("resources", Locale.locale, new ResourceBundleWithEncording());
That's it.
However, since the default character code of ResourceBundle is finally UTF-8 in Java 9, the future turn will be It doesn't look like much.
-I want to write Java property files in UTF-8 encoding -[Characters are garbled when getting the value of application.properties in Eclipse](https://ja.stackoverflow.com/questions/27787/eclipse%E3%81%A7application-properties%E3%81%AE%E5%80 % A4% E3% 82% 92% E5% 8F% 96% E5% BE% 97% E3% 81% 99% E3% 82% 8B% E3% 81% A8% E6% 96% 87% E5% AD% 97 % E5% 8C% 96% E3% 81% 91% E3% 81% 99% E3% 82% 8B)