java 中文读取

在不同编码方式下中文字符的16进制值

编码方式不同,中文的16进制值也是不同的,例如“中”字
在这里插入图片描述

package JavaIOTest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class ShowChineseEncodeValue {
    public static void main(String[] args) {
        String chinese = "中";
        showCode(chinese);
    }
    private static void showCode(String str){
        String[] encodes = new String[]{"BIG5","GBK","GB2312","UTF-8","UTF-16","UTF-32"};
        for (String encode : encodes){
            showCode(str,encode);
        }
    }
    private static void showCode(String str,String encode){
        try{
            System.out.printf("字符:\"%s\"在编码方式%s下的16进制值是\n",str,encode);
            //getBytes()得到系统默认编码的字节数组
            //getBytes("GBK")得到指定编码的字符数组
            byte[] bs = str.getBytes(encode);
//            System.out.println(Arrays.toString(bs));
            for (byte b : bs){
                int i = b&0xff;//
                System.out.print(Integer.toHexString(i)+"\t");
            }
            System.out.println();
        }catch (UnsupportedEncodingException e){
            System.out.printf("UnsupportedEncodingException: %s编码方式无法解析字符%s\n", encode, str);
        }
    }
}

用字节流读取中文

1、首先需要知道文本是以那种编码方式保存字符的,
2、用字节流读取文本后用对应的编码方式去识别就能得到正确的字符
准备一个textFile.txt文件,保存的编码格式为UTF-8,下面使用字节流进行读取

package JavaIOTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadChineseByFileInputeStream {
    public static void main(String[] args) {
        File textFile = new File("d:/xyz/z/zzz/zzzz/textFile.txt");
        byte[] bytes = new byte[(int)textFile.length()];
        try(FileInputStream fileInputStream = new FileInputStream(textFile) ){
            fileInputStream.read(bytes);
            System.out.println(new String(bytes,"UTF-8"));
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

使用FileReader读取文本

FileReader得到的是字符,所以一定是已经把字节根据某种编码识别成了字符了
而FileReader使用的编码方式是Charset.defaultCharset()的返回值。FileReader是不能手动设置编码方式的,为了使用其他的编码方式,只能使用InputStreamReader来代替,像这样:

new InputStreamReader(new FileInputStream(f),Charset.forName("UTF-8")); 
package JavaIOTest;

import java.io.*;
import java.nio.charset.Charset;

public class ReadChineseByFileReader {
    public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {
        File textFile = new File("d:/xyz/z/zzz/zzzz/textFile.txt");
        System.out.println("默认的编码方式是"+ Charset.defaultCharset());
        char[] chars = new char[(int)textFile.length()];
        try(FileReader fileReader = new FileReader(textFile)){
            fileReader.read(chars);
            System.out.printf("FileReader会使用默认的编码方式%s,识别出的字符是%n",Charset.defaultCharset());
            System.out.println(new String(chars));
        }catch (IOException e){
            e.printStackTrace();
        }
        try (InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(textFile),Charset.forName("utf-8"))){
            inputStreamReader.read(chars);
            System.out.printf("InputStreamReader指定编码方式UTF-8识别出的字符是\n");
            System.out.println(new String(chars));
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

posted @ 2019-07-30 13:19  消灭猕猴桃  阅读(188)  评论(0编辑  收藏  举报