Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在UTF-8编码代码中,使用带有重音字符的字符串,这些字符取自ISO-8859-1编码的文件_Java_Encoding_Utf 8 - Fatal编程技术网

Java 在UTF-8编码代码中,使用带有重音字符的字符串,这些字符取自ISO-8859-1编码的文件

Java 在UTF-8编码代码中,使用带有重音字符的字符串,这些字符取自ISO-8859-1编码的文件,java,encoding,utf-8,Java,Encoding,Utf 8,有人问过类似的问题,但我找不到解决问题的办法 我有一个属性文件,即config.properties,以ISO-8859-1编码,具有以下内容: config1 = some value with âccénted characters 我有一个加载属性的类和一个获取属性值的方法 public class EnvConfig { private static final Properties properties = new Properties(); static {

有人问过类似的问题,但我找不到解决问题的办法

我有一个属性文件,即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),代码需要正确的带重音的字符串:一些值带有带字母的字符

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编码属性文件中配置的带重音符号的字符串?

经过数小时的搜索后,发现我的编码问题是由项目POM中设置为true的资源过滤引起的:

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

src/main/resources
真的

将此设置为false可修复此问题。我仍然需要找到一种方法使其在启用过滤的情况下工作,所以我将尝试解决它。其他问题/答案中有一些线索,如。谢谢。

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