简体   繁体   English

解码UTF-8字符串,然后在8859-2中将其编码为斯洛伐克字母

[英]Decoding UTF-8 String, then encoding it in 8859-2 for Slovakian alphabet

I'm trying to translate strings into Slovakian alphabet (8859-2 encoding) via a matching table I created. 我正在尝试通过我创建的匹配表将字符串转换为斯洛伐克字母(8859-2编码)。 Problem is some characters do not actually change and the output string shows me "?" 问题是某些字符实际上并未更改,并且输出字符串显示了“?” for some slovakian character. 对于某些斯洛伐克字符。

How do I do that? 我怎么做? I did something like this first : 我首先做了这样的事情:

        File tempFile = File.createTempFile("buffer", ".tmp");
        FileWriter fw = new FileWriter(tempFile);
        Reader fr = new FileReader(fileOriginal);
        BufferedReader br = new BufferedReader(fr);

        while (br.ready()) {
            fw.write(br.readLine().replace("#/e#" , "ě").replace("#>E#" , "É")
        }

but some Characters are not correctly replaced. 但是某些字符没有正确替换。

For example, in this String : "allo #>e# de #>E#" (just an example), it should give me "allo ě de É" but it gives me "allo ? de ?" 例如,在此字符串中:“ allo#> e#de#> E#”(仅作为示例),它应该给我“ alloědeÉ”,但是却给我“ allo?de?”。 because UTF-8 doesn't recognize these characters. 因为UTF-8无法识别这些字符。 What do I need to do for my java program to recognize it ? 我的Java程序需要做什么才能识别它?

Thanks in advance. 提前致谢。

Does something like this should works for you? 这样的事情适合您吗?

BufferedReader br = new BufferedReader(new InputStreamReader(fr,"UTF-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fw,"8859-2"));
while (br.ready()) {
    bw.write(br.readLine());
}

This code reads a file content encoded in UTF-8 and writes by encoding in 8859-2. 此代码读取以UTF-8编码的文件内容,并通过8859-2编码进行写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM