Search code examples
javautf-8ansi

Cannot convert and save UTF-8 string to ANSI in java


Here is my code. I have to write string to console in UTF-8 but save the string in ANSI. When I open file it's in UTF-8. What do I do?

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
String message = bufferedReader.readLine();
bufferedReader.close();
String utfString = new String(message.getBytes(), "UTF-8");
String ansiMessage = new String(utfString.getBytes(), "WINDOWS-1251");
writeToFile(ansiMessage, "ANSI.txt", "WINDOWS-1251");
private static void writeToFile(String string, String path, String enc) throws IOException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), enc));
    writer.write(string);
    writer.close();
}

Solution

  • First, getBytes() returns the bytes of the string in a default charset, which is usually UTF-16. Second, new String(bytes[], string) interpret the bytes as a string in the charset provided, it doesn't convert them. So:

    new String(message.getBytes(), "UTF-8")

    Try to read a UTF-16 string as UTF-8, bad. Then:

    new String(utfString.getBytes(), "WINDOWS-1251")

    Try to read the resulting string as WINDOWS-1251, equally bad.

    I'm sure at this point your string is destroyed.

    You can just call getBytes(Charset) to get the bytes of your string in the charset you want. But in your case you don't even need to do that, because your writeToFile(...) method already does charset conversion when writing to the file, so you can just give it the original message.