s-day09--使用FileReader读取中文产生的乱码问题
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
package com.inmind.transferstream02;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
|
||||||
|
/*
|
||||||
|
使用字符流(FileReader),读取电脑中的文件数据到java内存
|
||||||
|
|
||||||
|
出现乱码的原因:中文数据在读写操作的前后的编码方式不一致.
|
||||||
|
注意:
|
||||||
|
1.UTF-8,字母和数字只占1个字节,而一个中文占3个字节,它的前128个字符完全兼容ASCII码表
|
||||||
|
2.GBK,字母和数字只占1个字节,而一个中文占2个字节,它的前128个字符完全兼容ASCII码表
|
||||||
|
*/
|
||||||
|
public class Demo05 {
|
||||||
|
public static void main(String[] args) throws Exception{
|
||||||
|
//程序默认FileReader采用的utf-8编码读取本地电脑中的GBK编码的内容,导致了乱码
|
||||||
|
FileReader fr = new FileReader("D:\\io_test\\file_gbk2.txt");
|
||||||
|
//一次读取一个字符或一个字符数组
|
||||||
|
char[] chars = new char[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = fr.read(chars)) != -1) {
|
||||||
|
//读多少个字符就展示多少个字符
|
||||||
|
System.out.println(new String(chars,0,len));
|
||||||
|
}
|
||||||
|
|
||||||
|
fr.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user