学习JAVA的IO流之转换流

处理流之二:
转换流的使用
1.转换流:属于字符流
InputstreamReader:将一个字 节的输入流转换为字符的输入流
Outputstreamwriter:将一个字符的输出流转换 为字节的输出流
2.作用:提供字书流与字符流之间的转换
3.解码:字节、字节数组—>字符数组、字符串
编码:字符数组、字符串—>字节、字节数组
4.字符集
ASCII: 美国标准信息交换码:用一个字节的7位可以表示。
IS08859-1: 拉丁码表。欧洲码表:用一个字节的8位表示。
GB2312:中国的中文编码表。最多两个字节编码所有字符
GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码
Unicode: 国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯- -的
字符码。所有的文字都用两个字节来表示。
UTF-8:变长的编码方式,可用1-4个字节来表示一个字符工

public class InputOutputSteam {
/**
 * 综合使用InputStreamReader和OutputStream
 * 
 */
	@Test
	public void Test1() throws IOException {
		//造文件,造流
		File F1 = new File("D:\\eclipse2\\eclipse\\IO流\\src\\NewIo.txt");
		File F2 = new File("D:\\eclipse2\\eclipse\\IO流\\src\\Newlo1.txt");
		
		FileInputStream fis = new FileInputStream(F1);
		FileOutputStream fos = new FileOutputStream(F2);
		
		InputStreamReader isr = new InputStreamReader(fis,"utf-8");
		OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
		
		//2读写过程
		char[] a =new char[20];
		int len ;
		while((len = isr.read(a)) != -1) {
			osw.write(a, 0, len);
		}
		//关闭流
		isr.close();
		osw.close();
	}
}
发布了29 篇原创文章 · 获赞 3 · 访问量 862

猜你喜欢

转载自blog.csdn.net/My_name_PeterLiu/article/details/104494835