1. 复习 字符编码:字符、字符串、字符数组---> 字节、字节数组(从我们能看得懂的--->我们看不懂的) 字符解码:字节、字节数组 ---> 字符、字符串、字符数组(从我们看不懂的--->我们能看得懂的) 2. 如果希望程序在读取文本文件时,不出现乱码,需要注意什么? > 解码时使用的字符集必须与当初编码时使用的字符集得相同。 > 拓展:解码集必须要与编码集兼容。比如:文件编码使用的是GBK,解码时使用的是utf-8。如果文件中只有abc等英文字符,此情况下 也不会出现乱码。因为GBK和utf-8都向下兼容了ASCII (或 ascii) 3. 转换流: ① 作用:实现字节与字符之间的转换 ② API: InputStreamReader:将一个输入型的字节流转换为输入型的字符流。 OutputStreamWriter:将一个输出型的字符流转换为输出型的字节流。 4. 关于字符集的理解 4.1 在存储的文件中的字符: ascii:主要用来存储a、b、c等英文字符和1、2、3、常用的标点符号。每个字符占用1个字节。 iso-8859-1:了解,每个字符占用1个字节。向下兼容ascii。 gbk:用来存储中文简体繁体、a、b、c等英文字符和1、2、3、常用的标点符号等字符。 中文字符使用2个字节存储的。向下兼容ascii,意味着英文字符、1、2、3、标点符号仍使用1个字节。 utf-8:可以用来存储世界范围内主要的语言的所有的字符。使用1-4个不等的字节表示一个字符。 中文字符使用3个字节存储的。向下兼容ascii,意味着英文字符、1、2、3、标点符号仍使用1个字节。 4.2 在内存中的字符: 一个字符(char)占用2个字节。在内存中使用的字符集称为Unicode字符集。
package chapter15_io_teacher.src.com.atguigu04.inputstreamreader; import org.junit.Test; import java.io.*; /** * ClassName: InputStreamReaderTest * Description: * * @Author 尚硅谷-宋红康 * @Create 11:14 * @Version 1.0 */ public class InputStreamReaderTest { @Test public void test1() throws IOException { //创建File对象 File file1 = new File("dbcp_utf-8.txt"); //创建流对象 FileInputStream fis = new FileInputStream(file1); // InputStreamReader isr = new InputStreamReader(fis);//此时使用的是IDEA默认的UTF-8的字符集 InputStreamReader isr1 = new InputStreamReader(fis,"utf-8");//显式的使用UTF-8的字符集 //读入操作 char[] cBuffer = new char[1024]; int len; while((len = isr1.read(cBuffer)) != -1){ String str = new String(cBuffer,0,len); System.out.print(str); } //关闭资源 isr1.close(); } /* * 读取到的数据出现了乱码。 * 因为dbcp_utf-8.txt文件使用的是utf-8的字符集进行的编码,所以在读取此文件时使用的解码集必须也是utf-8, * 否则会出现乱码! * */ @Test public void test2() throws IOException { //创建File对象 File file1 = new File("dbcp_utf-8.txt"); //创建流对象 FileInputStream fis = new FileInputStream(file1); InputStreamReader isr1 = new InputStreamReader(fis,"gbk");//显式的使用gbk的字符集 //读入操作 char[] cBuffer = new char[1024]; int len; while((len = isr1.read(cBuffer)) != -1){ String str = new String(cBuffer,0,len); System.out.print(str); } //关闭资源 isr1.close(); } @Test public void test3() throws IOException { //创建File对象 File file1 = new File("dbcp_gbk.txt"); //创建流对象 FileInputStream fis = new FileInputStream(file1); InputStreamReader isr1 = new InputStreamReader(fis,"gbk");//显式的使用gbk的字符集 //读入操作 char[] cBuffer = new char[1024]; int len; while((len = isr1.read(cBuffer)) != -1){ String str = new String(cBuffer,0,len); System.out.print(str); } //关闭资源 isr1.close(); } /* * 需求:将gbk格式的文件转换为utf-8格式的文件存储。 * */ @Test public void test4() throws IOException { //1. 造文件 File file1 = new File("dbcp_gbk.txt"); File file2 = new File("dbcp_gbk_to_utf8.txt"); //2. 造流 FileInputStream fis = new FileInputStream(file1); //参数2对应的是解码集,必须与dbcp_gbk.txt的编码集一致。 InputStreamReader isr = new InputStreamReader(fis,"GBK"); FileOutputStream fos = new FileOutputStream(file2); //参数2指明内存中的字符存储到文件中的字节过程中使用的编码集。 OutputStreamWriter osw = new OutputStreamWriter(fos,"utf8"); //3. 读写过程 char[] cBuffer = new char[1024]; int len; while((len = isr.read(cBuffer)) != -1){ osw.write(cBuffer,0,len); } System.out.println("操作完成"); //4. 关闭资源 osw.close(); isr.close(); } }