一、转换流(InputStreamReader、OutputStreamWriter)简介
1.1 简单介绍
1. 转换流为处理流中的一类,作用于字节流之上,同时针对与处理数据类型的分类而言属于字符流
InputStreamReader是字节流到字符流的桥梁:它读取字节并使用指定的字符集将其解码为字符。它使用的字符集可以通过名称指定,也可以显式地给
出,或者可以使用默认字符集。
OutputStreamWriter是字符流和字节流之间的桥梁:写入它的字符使用指定的字符集编码为字节。它使用的字符集可以通过名称来指定,也可以显式地
给出,或者可以接受默认字符集。
2. 作用:提供字节流与字符流之间的转换
> ① InputStreamReader:使用指定的字符集读取并解码来自字节流的数据,由字节流转换为字符流
> ② OutputStreamWriter:使用指定的字符集编码并写入持久化存储设备,由字符流转换为字节流
3. 解码:字节、字节数组 -----> 字符数组、字符串
编码:字符数组、字符串 -----> 字节、字节数组
4. 字符集
ASCII:美国标准信息交换码
ISO8859-1:拉丁码表(欧洲码表)
GB2312:中文编码表(两个字节编码所有的字符)
GBK: GB2312的升级版,融合更多的中文文字字符
Unicode: 国际标准码,融合了目前人类使用的所有字符
UTF-8: 变长的编码方式,可用1-4个字节来表示一个字符
1.2 继承关系以及整体框架
public class OutputStreamWriter extends Writer
|---java.lang.Object
|---java.io.Writer
|---java.io.OutputStreamWriter
public class InputStreamReader extends Reader
|---java.lang.Object
|---java.io.Reader
|--- java.io.InputStreamReader

二、转换流的使用示例
读取文本文件并使用指定字符集编码写入文件
public void disposeStreamTest(){
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File inputFile = new File("C:\\Users\\youngster\\Desktop\\数据库系统概述.txt");
File outputFile = new File("C:\\Users\\youngster\\Desktop\\DataBaseDesc.txt");
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
isr = new InputStreamReader(fis);
osw = new OutputStreamWriter(fos,"gbk");
char[] chars = new char[20];
int readLen;
while ((readLen = isr.read(chars)) != -1) {
osw.write(chars, 0, readLen);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}