Question

I'm trying to convert UTF-8 text file into Windows-1250. I'm using Java 6 API.

I've used code below, but the result is definitely not a Cp1250.

import java.io.*; 

public class testing {
    public static void main (String[] args) throws IOException {   

        InputStream  input = null;
        OutputStreamWriter output = null;   

        File destinationFile = new File("C:\\test\\Cp1250.txt");   

        try {      
            input = new FileInputStream("C:\\test\\utf-8.txt");
            output = new OutputStreamWriter(new FileOutputStream(destinationFile), "Windows-1250");

            while (input.read() != -1) {
                output.write(input.read());
            }

        } catch (Exception e) {
            e.printStackTrace(); 
        } finally {
            input.close();
            output.close(); 
        }      
    }
}
Was it helpful?

Solution

You need an InputStreamReader, which knows that the data from the FileInputStream must be interpreted with the UTF-8 charset.

Here is an example. I have omitted the closing of the resources for brevity.

    FileInputStream input = new FileInputStream(file);
    InputStreamReader reader = new InputStreamReader(input, "utf-8");
    FileOutputStream output = new FileOutputStream(destinationFile);
    OutputStreamWriter writer = new OutputStreamWriter(output, "Windows-1250");

    int read = reader.read();
    while (read != -1)
    {
        writer.write(read);
        read = reader.read();
    }

And another thing: In your while loop you have two calls to input.read, but you call output.write() only once. This means you only write half of the bytes which you have read.

OTHER TIPS

Here is how you do it when using Java 7:

final Path src = Paths.get("C:\\test\\utf-8.txt");
final Path dst = Paths.get("C:\\test\\Cp1250.txt");

try (
    BufferedReader reader = Files.newBufferedReader(src, StandardCharsets.UTF_8);
    BufferedWriter writer = Files.newBufferedWriter(dst, 
        Charset.forName("windows-1252"));
) {
    String line;
    while ((line = reader.readLine()) != null) {
        writer.write(line);
        writer.newLine();
    }
    writer.flush();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top