1.转换流涉及到的类:
1.转换流:属于字符流
InputStreamReader:将一个字节的输入流转换为字符的输入流
解码:字节、字节数组 —>字符数组、字符串OutputStreamWriter:将一个字符的输出流转换为字节的输出流
编码:字符数组、字符串 —> 字节、字节数组
2.作用:
提供字节流与字符流之间的转换
3.图示:

4.典型实现:
//综合使用InputStreamReader和OutputStreamWriter
@Test
public void test2(){
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
//1.造文件、造流
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp-gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
isr = new InputStreamReader(fis);
osw = new OutputStreamWriter(fos,"gbk");
//读写操作
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf)) != -1){
osw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(osw != null){
//4.关流
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(isr != null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5.说明:

6.常见的编码表

7.对后面学习的启示

版权声明:本文为qq_45554909原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。