Java-UTF-8编码的数据写入到文件中

Java-UTF-8编码的数据写入到文件中

Tags: Java

摘要

Java-UTF-8编码的数据写入到文件中

UTF-8编码的数据写入到文件中

package cn.javacore.io.file;

import java.io.*;

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

        try {
            File fileDir = new File("/home/zhu/javacore.cn/file-w.txt");

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

            out.append("にほんご").append("\r\n");
            out.append("繁體").append("\r\n");
            out.append("简体中文").append("\r\n");
            out.append("Hello World").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());
        }
    }
}