您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Java 如何通过ResourceBundle在资源属性中使用UTF-8

Java 如何通过ResourceBundle在资源属性中使用UTF-8

ResourceBundle#getBundle()封面下使用PropertyResourceBundle时,.properties被指定的文件认情况下Properties#load(InputStream),这反过来使用它来加载那些属性文件。根据javadoc认情况下将其读取为ISO-8859-1。

public void load(InputStream inStream) throws IOException

从输入字节流中读取属性列表(键和元素对)。输入流采用load(Reader)中指定的面向行的简单格式,并假定使用ISO 8859-1字符编码;即每个字节是一个latin1字符。不在latin1中的字符以及某些特殊字符使用Java?语言规范第3.3节中定义的Unicode转义在键和元素中表示。

因此,你需要将它们另存为ISO-8859-1。如果你有任何超出ISO-8859-1范围的字符,并且无法使用\uXXXX,因此不得不将文件另存为UTF-8,那么你需要使用native2ascii工具转换UTF-8保存的属性文件ISO-8859-1保存的属性文件,其中所有未发现的字符都转换为\uXXXX格式。下面的示例将UTF-8编码的属性文件text_utf8.properties转换为有效的ISO-8859-1编码的属性文件text.properties。

native2ascii-编码UTF-8 text_utf8.properties text.properties

当使用健全的IDE(例如Eclipse)时,.properties在基于Java的项目中创建文件并使用Eclipse自己的编辑器时,这已经自动完成。Eclipse将透明地将超出ISO-8859-1范围的字符转换为\uXXXX格式。另请参见下面的屏幕截图(请注意底部的“属性”和“源”选项卡,单击以查看大图):

另外,你还可以创建一个自定义ResourceBundle.Control实现,其中你可以使用来将属性文件显式读取为UTF-8 InputStreamReader,这样你就可以将它们另存为UTF-8,而无需麻烦native2ascii。这是一个启动示例:

public class UTF8Control extends Control {
    public ResourceBundle newBundle
        (String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws illegalaccessexception, InstantiationException, IOException
    {
        // The below is a copy of the default implementation.
        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, "properties");
        ResourceBundle bundle = null;
        InputStream stream = null;
        if (reload) {
            URL url = loader.getResource(resourceName);
            if (url != null) {
                URLConnection connection = url.openConnection();
                if (connection != null) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        } else {
            stream = loader.getResourceAsStream(resourceName);
        }
        if (stream != null) {
            try {
                // Only this line is changed to make it to read properties files as UTF-8.
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        }
        return bundle;
    }
}

可以如下使用:

ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.text", new UTF8Control());
java 2022/1/1 18:21:51 有529人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶