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