Decoding UTF-8 String, then encoding it in 8859-2

2019-08-17 09:18发布

I'm trying to translate strings into Slovakian alphabet (8859-2 encoding) via a matching table I created. 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 ?" because UTF-8 doesn't recognize these characters. What do I need to do for my java program to recognize it ?

Thanks in advance.

1条回答
beautiful°
2楼-- · 2019-08-17 10:00

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.

查看更多
登录 后发表回答