进阶day10-使用FileReader读取中文产生的乱码问题
This commit is contained in:
29
javaSE-day10/src/com/inmind/transfer_stream02/Demo07.java
Normal file
29
javaSE-day10/src/com/inmind/transfer_stream02/Demo07.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.inmind.transfer_stream02;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/*
|
||||
使用字符流,读取电脑中的文件数据到java内存,并打印内容
|
||||
|
||||
出现乱码的原因:中文数据在读写操作的前后的编码方式不一致(gbk utf-8).
|
||||
|
||||
注意:
|
||||
1.UTF-8,字母和数字只占1个字节,而一个中文占3个字节,它的前128个字符完全兼容ASCII码表
|
||||
2.GBK,字母和数字只占1个字节,而一个中文占2个字节,它的前128个字符完全兼容ASCII码表
|
||||
|
||||
*/
|
||||
public class Demo07 {
|
||||
public static void main(String[] args) throws IOException {
|
||||
// FileReader fr = new FileReader("D:\\io_test\\file_gbk.txt");
|
||||
FileReader fr = new FileReader("D:\\io_test\\file.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