Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

java, StandardCharsets utf-16 issue

I’m trying to undetstand why the results are different while I’m trying to write a test string using different encoding.
For StandardCharsets.UTF_16LE the result is "test" (seems correct), while
for StandardCharsets.UTF_16BE the resul is " t e s t" (seems wrong).
Can someone please explane why in the case of UTF_16BE the result is having unnecessary spaces between letters?

    String filename="C:\\Users\\name\\Downloads\\debugging.txt";
    String str="test";

    File fl = new File(filename);

    try (FileOutputStream fos = new FileOutputStream(fl);

         //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_16LE))) {//seems does work
         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_16BE))) {//seems does not work

        bw.write(str);
    } catch (IOException ignored) {
        //some actions
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Javadoc says:

When decoding, the UTF-16BE and UTF-16LE charsets interpret the
initial byte-order marks as a ZERO-WIDTH NON-BREAKING SPACE; when
encoding, they do not write byte-order marks.

Without a BOM, editors have to guess the correct encoding and they could be not so clever. Some editor could simply not support some encoding. So it depends on the editor that you use to read the file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading