Writing in Shift-JIS encoding in Java

title: Writing in Shift-JIS Encoding in Java slug: writing-shift-jis-encoding-java date: February 15, 2022 tags: java, encoding, shift-jis

Do you need to write text in Shift-JIS encoding in your Java program? Shift-JIS is a character encoding commonly used for Japanese text. In this blog post, we will explore how to write text in Shift-JIS encoding using Java.

Setting the Encoding

To write text in Shift-JIS encoding, you need to set the encoding when writing to a file or output stream. Here’s an example of how to do it:

import java.io.*;

public class ShiftJISEncodingExample {

    public static void main(String[] args) {
        try {
            // Create a FileOutputStream and specify the encoding
            FileOutputStream fos = new FileOutputStream("output.txt");
            OutputStreamWriter writer = new OutputStreamWriter(fos, "Shift-JIS");

            // Write the text in Shift-JIS encoding
            writer.write("こんにちは、世界!");
            
            // Remember to flush and close the writer
            writer.flush();
            writer.close();
            
            System.out.println("Text written in Shift-JIS encoding successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the example above, we create a FileOutputStream to write to a file named “output.txt”. We pass this FileOutputStream to the OutputStreamWriter constructor and specify the encoding as “Shift-JIS”. Then, we use the write() method of the OutputStreamWriter to write the text in Shift-JIS encoding. Finally, we flush and close the writer.

Conclusion

Writing in Shift-JIS encoding in Java is straightforward. By setting the encoding when writing to a file or output stream, you can write Japanese text in Shift-JIS format. This can be useful when working with Japanese systems or when you need to generate files with Shift-JIS encoding. Happy coding!

Don’t forget to follow us for more Java tutorials and tips. #java #encoding #shiftjis