Java-读取UTF-8编码的文件

Java-读取UTF-8编码的文件

Tags: Java

摘要

Java-读取UTF-8编码的文件

UTF-8文件

读取UTF8编码的文件例子

package cn.javacore.io.file;

import java.io.*;

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

        try {
            File fileDir = new File("/home/zhu/javacore.cn/file.txt");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream(fileDir), "UTF8"));
            String str;
            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }
            in.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());
        }

    }
}

输出

にほんご
繁體
简体中文
Hello World