Question

I want to write some special characters from UTF-16 to a file. I tried some methods but I don't get what is going on behind scenes. Below code writes 'u' character to file. How can I write special charaters from UTF-16?

    File f = new File("test");
    FileOutputStream fos = new FileOutputStream(f);
    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-16");
    fos.write(16757);
Was it helpful?

Solution

You would have to use write() of OutputStreamWriter to write the encoded chars:

Change:

fos.write(16757);

To:

osw.write(16757);

OTHER TIPS

try maybe something like this:

    File f = new File("test");
    FileOutputStream fos = new FileOutputStream(f);
    fos.write("16757").getBytes("UTF-16"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top