UTF-8でエンコードされたデータをファイルに書き込む方法 - Java

UTF-8エンコードデータをファイルに書き込む方法– Java

これは、UTF-8 encoded dataをテキストファイルに書き込む方法を示すJavaの例です–「c:\temp\test.txt

P.S Symbol “??” is some “UTF-8” data in Chinese and Japanese

package com.example;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

public class test {
    public static void main(String[] args){

      try {
        File fileDir = new File("c:\\temp\\test.txt");

        Writer out = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream(fileDir), "UTF8"));

        out.append("Website UTF-8").append("\r\n");
        out.append("?? UTF-8").append("\r\n");
        out.append("??????? UTF-8").append("\r\n");

        out.flush();
        out.close();

        }
       catch (UnsupportedEncodingException e)
       {
        System.out.println(e.getMessage());
       }
       catch (IOException e)
       {
        System.out.println(e.getMessage());
        }
       catch (Exception e)
       {
        System.out.println(e.getMessage());
       }
    }
}

結果

utf8 result