From cbb90a035e7d1516acf3019f4681ff7ab86af3e5 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Mon, 23 Mar 2026 14:23:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E9=98=B6day10-=E4=BD=BF=E7=94=A8FileR?= =?UTF-8?q?eader=E8=AF=BB=E5=8F=96=E4=B8=AD=E6=96=87=E4=BA=A7=E7=94=9F?= =?UTF-8?q?=E7=9A=84=E4=B9=B1=E7=A0=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/inmind/transfer_stream02/Demo07.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 javaSE-day10/src/com/inmind/transfer_stream02/Demo07.java diff --git a/javaSE-day10/src/com/inmind/transfer_stream02/Demo07.java b/javaSE-day10/src/com/inmind/transfer_stream02/Demo07.java new file mode 100644 index 0000000..45c482e --- /dev/null +++ b/javaSE-day10/src/com/inmind/transfer_stream02/Demo07.java @@ -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(); + } +}