Writing in UTF-32 encoding in Java

Java is a versatile programming language that provides support for various character encodings, including UTF-32. UTF-32 is a Unicode encoding that uses 32 bits (4 bytes) to represent each character.

In Java, you can write in UTF-32 encoding by utilizing the appropriate encoders and decoders from the java.nio.charset package. Here’s an example to demonstrate how to read and write UTF-32 encoded text in Java:

Reading UTF-32 Encoded Text

import java.io.IOException;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class UTF32Reader {

    public static void main(String[] args) {
        String fileName = "utf32_encoded.txt";
        try (FileInputStream fileInputStream = new FileInputStream(fileName);
             FileChannel fileChannel = fileInputStream.getChannel()) {

            // Read UTF-32 encoded text into ByteBuffer
            ByteBuffer buffer = ByteBuffer.allocate((int) fileChannel.size());
            fileChannel.read(buffer);
            buffer.flip();

            // Decode ByteBuffer to String using UTF-32 Charset
            String text = Charset.forName("UTF-32").newDecoder().decode(buffer).toString();
            System.out.println(text);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Writing UTF-32 Encoded Text

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class UTF32Writer {

    public static void main(String[] args) {
        String textToWrite = "Hello, world! 你好,世界!";

        String fileName = "utf32_encoded.txt";
        try (FileOutputStream fileOutputStream = new FileOutputStream(fileName);
             FileChannel fileChannel = fileOutputStream.getChannel()) {

            // Encode String to UTF-32 ByteBuffer
            ByteBuffer buffer = Charset.forName("UTF-32").newEncoder().encode(CharBuffer.wrap(textToWrite));

            // Write ByteBuffer to file
            fileChannel.write(buffer);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the above examples, we use the FileChannel class to read from and write to the file. The Charset.forName("UTF-32") method is used to create the appropriate decoder and encoder for UTF-32 encoding. The ByteBuffer class is used to store the text data.

To read UTF-32 encoded text, we read the contents of the file into a ByteBuffer and then decode it to a String using the UTF-32 Charset.

To write UTF-32 encoded text, we encode the text to a ByteBuffer using the UTF-32 Charset and then write the ByteBuffer to the file using the FileChannel.

Remember to handle any IO exceptions that may occur during reading or writing operations.

#java #utf32 #unicode