0

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 :).

skonline90
  • 53
  • 7

2 Answers2

3

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);
Ferrybig
  • 18,194
  • 6
  • 57
  • 79
1

You could Use BufferedWriter:

public class SampleCode {

    public static void main(String[] args) throws IOException {
        String aString = "File contents";
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("outfilename"), "UTF-32"));
        try {
            out.write(aString);
        } finally {
            out.close();
        }

    }
}

Or you could use

Analogously, the class java.io.OutputStreamWriter acts as a bridge between characters streams and bytes streams. Create a Writer with this class to be able to write bytes to the file:

Writer out = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-32");

Or you can also use String format like below:

public static String convertTo32(String toConvert){
    for (int i = 0; i < toConvert.length(); ) {
        int codePoint = Character.codePointAt(toConvert, i);
        i += Character.charCount(codePoint);
        //System.out.printf("%x%n", codePoint);
        String utf32 = String.format("0x%x%n", codePoint);
        return utf32;
    }
    return null;
}

See How can I convert UTF-16 to UTF-32 in java?.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Subash J
  • 2,028
  • 3
  • 13
  • 27
  • While your first solution is good, the `convertTo32` is flawed, a string in java always has the UTF16 encoding internally, and cannot represent something else – Ferrybig Mar 22 '18 at 17:41
  • 1
    Also, `String utf32` should be declared above the loop, and `return utf32;` should be after the loop, Otherwise, there is no point in having the loop at all. – Remy Lebeau Mar 22 '18 at 23:43
  • When you directly use [someone-else's answer](https://stackoverflow.com/a/36394647/1542723), make sure to properly attribute them, else it's called plagiarism. – Ferrybig Mar 23 '18 at 08:09