Search code examples
javaunicoderandomaccessfile

Java: Write to a file in UTF-32 Format


Is it possible to write Strings to a file in utf-32 format? For example: the RandomAccessFile class only offers the writeUTF() Method, which writes the String in a modifed UTF-8 format.

Lets say my task is to write every existing unicode character into a file :).


Solution

  • You should convert your string to bytes in the UTF-32 format, and then write write those bytes to your random file

    RandomAccessFile file = ...
    String str = "Hi";
    byte[] bytes = str.getBytes("UTF-32");
    file.write(bytes);