【问题标题】:In UTF-8 encoded code, use a string with accented characters taken from a file encoded in ISO-8859-1在 UTF-8 编码代码中,使用带有重音字符的字符串,该字符串取自以 ISO-8859-1 编码的文件
【发布时间】:2020-01-18 11:26:42
【问题描述】:

有人问了非常相似的问题,但我找不到解决问题的方法。

我有一个属性文件,即 config.properties以 ISO-8859-1 编码,具有以下内容:

config1 = some value with âccénted characters

我有一个加载属性的类和一个获取属性值的方法

public class EnvConfig {
    private static final Properties properties = new Properties();

    static {        
        initPropertiesFromFile();
    }

    private static void initPropertiesFromFile() {
        InputStream stream;

        try {
            stream = EnvConfig.class.getResourceAsStream("/config/config.properties");
            properties.load(new InputStreamReader(stream, Charset.forName("ISO-8859-1")));
            // Tried that as well instead of the previous line: properties.load(stream);
        } catch (Exception e) {
            // Do something
        } finally {
            stream.close();
        }
    }

    public static String getProperty(String key, String defaultValue) {
        try {
            System.out.println(Charset.defaultCharset()); // Prints UTF-8
            // return new String(properties.getProperty(key).getBytes("ISO-8859-1")); // Returns some value with �cc�nted characters
            // return new String(properties.getProperty(key).getBytes("UTF-8")); // Returns some value with �cc�nted characters
            // return new String(properties.getProperty(key).getBytes("ISO-8859-1"), "UTF-8") // Returns some value with �cc�nted characters
            return properties.getProperty(key, defaultValue); // Returns some value with �cc�nted characters
        } catch (Exception e) {
            // Do something
            return defaultValue;
        }
    }
}

我的代码对属性值 (String) 进行了处理,并且该代码需要正确的带重音符号的字符串:带有 âccénted 字符的某些值

public void doSomething() {
    ...
    EnvConfig.getProperty("config1"); // I need the exact same value as configured in the properties file: some value with âccénted characters; currently get some value with �cc�nted characters
    ...
}

项目采用 UTF-8(Java 文件以 UTF-8 编码)并且项目属性/设置 (pom) 设置为 UTF-8。

我缺少什么,我该如何实现?我知道没有“UTF-8 格式的字符串”这样的东西,因为字符串只是 UTF-16 代码单元的序列。但是我怎样才能在我的 UTF-8 编码代码/项目中简单地拥有相同的“可用”输出,即带有重音符号的字符串,如 ISO-8859-1 编码属性文件中配置的那样? p>

【问题讨论】:

  • 您的阅读代码几乎肯定是正确的。问题可能是您用于打印输出的任何内容都不支持重音字符或配置不正确。你如何运行你的代码?在什么操作系统上?
  • 我在 Eclipse 中运行它,在 Windows 上,使用 TestNG(代码在测试中运行)。谢谢
  • 如果您只是将字符串 "some value with âccénted characters" 放入代码中(作为字符串文字)并打印:System.out.println("some value with âccénted characters");,会发生什么?
  • 它打印得很好,我正确地看到了重音字母:带有 âccénted 字符的一些值
  • 那么您的设置出现了其他问题。您如何验证配置文件使用的是 ISO-8859-1?请注意,Java 中的技术上 .properties 文件被指定为使用 ISO-8859-1,但某些库(如 Spring)使用 UTF-8 来代替读取它们(因此使它们“不是真实的”属性文件),因此某些工具(例如 IDE、构建系统)可能会将它们视为 ISO-8859-1,而其他工具可能会将其视为 UTF-8。

标签: java encoding utf-8


【解决方案1】:

经过几个小时的搜索,原来我的编码问题是由项目的 POM 中的资源过滤设置为 true 引起的:

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

将此设置为 false 可解决此问题。我仍然需要找到一种方法让它在启用过滤的情况下工作,所以我会尝试弄清楚。其他问题/答案中有一些线索,例如Wrong encoding after activating resource filtering。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2010-12-02
    • 2023-04-01
    • 2011-05-22
    • 2014-06-09
    相关资源
    最近更新 更多