Java ResourceBundles 无法正确读取 utf-8 字符 - 升级 Eclipse 后

标签 java eclipse utf-8 resourcebundle

我有一个可以正确运行很长时间的项目。几天前,我将 Eclipse 版本更新为 Kepler 版本,从那时起 - 我的属性文件无法正确读取。 希伯来语字母的读法如下:“××× ×©× ×שת×ש ×ס×ס××”。

我认为这些文件不知何故被破坏了,所以我将它们复制粘贴到最简单的txt文件中,然后再次添加到项目中。如果我只是简单地读取它们,它们就会被正确读取,但如果我继续使用 ResourceBundle 实现,它们就不会被正确读取。

有人知道如何解决这个问题吗? 我将平台和文件设置更改为 utf-8 编码 - 任何我能想到的地方...

这是资源代码:


        public class ResourceManager {

            protected final Logger logger = Logger.getLogger(this.getClass().getName());
            public static final String FILE_PREFIX = "file::";
            private static Locale locale = null;
            private static Map resourcesTable = new HashMap();

            // Static initializer of the Local object - this currently return "iw"
            static {
                locale = new Locale(ApplicationConfiguration.getInstance().getProperty("user.language"));
            }

            /**
             * Return the Resources object according to the base name and Locale.
             */
            private static Resources getResource(String baseName) {
                Resources resource = (Resources) resourcesTable.get(baseName);
                if (resource == null) {
                    resource = new Resources(baseName, locale);
                    resourcesTable.put(baseName, resource);
                }
                return resource;
            }

        //more code...

    /* This is the problematic method - but the problem starts even before when adding the resource to the resources map */
            private static String getFormatedMessage(String baseName, String messageKey, Object... args) {
                Resources resource = getResource(baseName);
                String msg = null;
                if (args == null || args.length == 0) {
                    msg = resource.getString(messageKey, "");
                } else {
                    msg = resource.format(messageKey, args);
                }

                return msg;
            }

        ....

        }

最佳答案

最终我没有创建新的工作区 - 我希望我的代码可以在任何平台、“任何条件下”工作。所以我通过更改代码以使用属性而不是资源来解决它。 作为输入参数,我使用了带有“utf-8”编码集的 Reader:


    ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
    Resource[] fileInJar = resourceResolver.getResources(filePath);
    Properties properties = new Properties();
    BufferedReader reader = new BufferedReader(new InputStreamReader(fileInJar[0].getInputStream(), "UTF-8"));
    properties.load(reader);

谢谢各位的回答!我真的很感激...

卡梅尔

关于Java ResourceBundles 无法正确读取 utf-8 字符 - 升级 Eclipse 后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20042040/

相关文章:

Java:HashMap 中的对象作为值被覆盖?

java - Groovy -collect() 返回强制转换异常

android - ActionBarSherlock 4.2 的构建问题

python - 当 AJAX 在 Django 中保存(并重新填充)我的表单时,为什么我会收到 '

Python 3 UnicodeDecodeError - 如何调试 UnicodeDecodeError?

java - 对于带有 netty 3.10.6 的套接字客户端,异常 : java. nio.channels.NotYetConnectedException

java - SpringFox Swagger 与 Springboot 应用程序集成

java - 使用 intelliJ IDEA 或 eclipse 替换/更新所有文件中的所有导入语句

java - 如何为 Eclipse 创建自定义 'new class wizard'?

mysql 正则表达式 utf-8 字符