Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Open File in UTF-8 and Write in Another File in UTF-16

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".

like image 852
Mohamed Benmahdjoub Avatar asked Nov 20 '25 23:11

Mohamed Benmahdjoub


2 Answers

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");
like image 99
aioobe Avatar answered Nov 23 '25 14:11

aioobe


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.

like image 35
fge Avatar answered Nov 23 '25 14:11

fge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!