简体   繁体   中英

Java Convert UTF-8 Text File to Cp1250

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(); 
        }      
    }
}

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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