转换流
1.转换流:属于字符流
- InputStreamReader:将一个字节的输入流转换为字符的输入流 解码
-
OutputStreamWriter:将一个字符的输出流转换为字节的输出流 编码
-
作用:提供字节流与字符流之间的转换
-
解码:字节、字节数组 —>字符数组、字符串
编码:字符数组、字符串 —> 字节、字节数组
public class InputStreamReaderTest {
/*
此时处理异常的话,仍然应该使用try-catch-finally
综合使用InputStreamReader和OutputStreamWriter
*/
@Test
public void test2() throws Exception {
//1.造文件、造流
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp_gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
//2.读写过程
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf)) != -1){
osw.write(cbuf,0,len);
}
//3.关闭资源
isr.close();
osw.close();
}