IO流-3
1.编码表
计算机要准确的存储和识别各种字符集符号,就需要进行字符编码,一套字符集必然至少有一套字符编码。 如果**编码和解码不是用一个编码表**就会出现**乱码**问题。
编码(加密):把看懂的-->看不懂解码(解密):看不懂-->把看懂的
1.1 常见的编码表
ASCII
用于显示现代英语,主要包括控制字符(回车键、退格、换行键等)和可显示字符(英文大小写字符、阿拉伯数字和西文符号)
基本的ASCII字符集,使用7位表示一个字符,共128字符。ASCII的扩展字符集使用8位表示一个字符,共256字符,方便支持欧洲常用字符。是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等GBK
最常用的中文码表。是在GB2312标准基础上的扩展规范,使用了双字节编码方案,共收录了21003个汉字,完全兼容GB2312标准,同时支持繁体汉字以及日韩汉字等Unicode
UTF-8编码:可以用来表示Unicode标准中任意字符,它是电子邮件、网页及其他存储或传送文字的应用 中,优先采用的编码。互联网工程工作小组(IETF)要求所有互联网协议都必须支持UTF-8编码。它使用一至四个字节为每个字符编码
编码规则:128个US-ASCII字符,只需一个字节编码拉丁文等字符,需要二个字节编码大部分常用字(含中文),使用三个字节编码其他极少使用的Unicode辅助字符,使用四字节编码
- ANSI
其实不是具体的编码表,它指代系统的默认编码表。例如:简体中文的Windows系统默认编码是GBK。 
2. 字符流
当需要读取/写入的数据是纯文本的形式时我们可以使用字符流来进行操作会更加方便。
2.1 字符输入流
所有字符输入流的父类是 java.io.Reader ,它是以字符为单位的输入流 。我们就以其子类FileReader为例进行学习。
2.1.1 FileReader 概述
FileReader 是用来从文件中读取数据的字符输入流。
2.1.2 FileReader创建对象
构造方法如下:
public FileReader(String fileName) throws FileNotFoundException //传入文件路径创建对象public FileReader(File file) throws FileNotFoundException //传入文件路径的File对象来创建流对象
范例:
public static void main(String[] args) throws FileNotFoundException {FileReader fr = new FileReader("C:\\Users\\root\\Desktop\\test\\汉字2.txt");FileReader fr2 = new FileReader(new File("C:\\Users\\root\\Desktop\\test\\汉字2.txt"));}
2.1.3 读取数据
2.1.3.1 一次读取一个字符
核心方法如下:
public int read() throws IOException //一次读取一个字符返回,如果读到文件末尾,返回值为-1
范例:
public static void main(String[] args) throws IOException {//创建流对象FileReader fr = new FileReader(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));//调用方法int ch;while((ch=fr.read())!=-1){System.out.println((char)ch);}//释放资源fr.close();}
2.1.3.2 一次读取一个字符数组
核心方法如下:
public int read(char cbuf[]) throws IOException //一次读取一个字符数组 返回值为-1时代表读到了末尾
范例:
public static void main(String[] args) throws IOException {//创建流对象FileReader fr = new FileReader(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));//读取char[] chars = new char[1024];int len;while((len=fr.read(chars))!=-1){//说明读到了内容System.out.println(chars);}//释放资源fr.close();}
2.2 字符输出流
所有字符输出流的父类是 java.io.Writer ,它是以字符为单位的输出流 。我们就以FileWriter为例进行学习。
2.2.1 FileWriter概述
FileWriter是用来往文件写入数据的字符输出流。
2.2.2 FileWriter对象创建
构造方法如下:
public FileWriter(String fileName) throws IOException //传入文件路径创建对象public FileWriter(File file) throws IOException //传入文件路径的File对象来创建流对象
范例:
public static void main(String[] args) throws IOException {FileWriter fw = new FileWriter("C:\\Users\\root\\Desktop\\test\\11.txt");FileWriter fw2 = new FileWriter(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));}
2.2.3 写入数据
2.2.3.1 一次写一个字符
核心方法如下:
public void write(int c) throws IOException //把一个字符写入目的地public void flush() throws IOException //把缓存区中的数据写入硬盘
范例:
public static void main(String[] args) throws IOException {//创建流对象FileWriter fw = new FileWriter(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));//写数据fw.write('三');fw.write('更');fw.flush();fw.write('草');fw.write('堂');//释放资源fw.close();}
2.2.3.2 一次写一个字符数组
核心方法如下:
public void write(char cbuf[]) throws IOException //把一个字符数组写入目的地
范例:
public static void main(String[] args) throws IOException {//创建流对象FileWriter fw = new FileWriter(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));//写数据char[] chars = "三更".toCharArray();fw.write(chars);fw.flush();chars = "草堂".toCharArray();fw.write(chars);//释放资源fw.close();}
2.2.3.3 一次写一个字符串
核心方法如下:
public void write(String str) throws IOException //把一个字符串写入目的地
范例:
public static void main(String[] args) throws IOException {//创建流对象FileWriter fw = new FileWriter(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));fw.write("三更草堂");fw.flush();//释放资源fw.close();}
2.2.3.4 如何抉择?
随机应变,有什么类型的数据就使用对应的重载。
练习
1.使用字符流实现纯文本文件的复制。
public static void main(String[] args) throws IOException {//1.创建流对象File file = new File("C:\\Users\\root\\Desktop\\test\\11.txt");FileReader fr = new FileReader(file);FileWriter fw = new FileWriter("C:\\Users\\root\\Desktop\\test\\22.txt");//2.循环读写char[] chars = new char[3];int len;while((len=fr.read(chars))!=-1){//把读到的内容写入新文件中fw.write(chars,0,len);//fw.flush();}//3.释放资源fw.close();fr.close();}
