How can I open a file in UTF-8 and write to another file in UTF-16?
I need an example because I'm having issues with some characters like 'é' and 'a'.
When writing "médic", I find in the file written "m@#dic".
You can create a reader as follows:
InputStream is = new FileInputStream(inputFile);
InputStreamReader in = new InputStreamReader(is, "UTF-8");
and a writer as follows:
OutputStream os = new FileOutputStream(outputFile);
OutputStreamWriter out = new OutputStreamWriter(os, "UTF-16");
Do this:
try (
final BufferedReader reader = Files.newBufferedReader(srcpath,
StandardCharsets.UTF_8);
final BufferedWriter writer = Files.newBufferedWriter(dstpath,
StandardCharsets.UTF_16BE);
) {
final char[] buf = new char[4096];
int nrChars;
while ((nrChars = reader.read(buf)) != -1)
writer.write(buf, 0, nrChars);
writer.flush();
}
NOTE: chosen big endian UTF-16. You didn't tell which one you wanted. If you want little endian, use UTF_16LE instead.
Also, if you want to skip the bom, just:
reader.read();
before looping for writing chars. The BOM is a single code point which happens to be in the BMP, so this will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With