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.