转换流:
InputStreamReader:字节到字符的桥梁。解码
OutputStreamWriter:字符到字节的桥梁。编码
流的基本规律
1.明白源和汇
源:InputStream、Reader
汇:OutputStream、Writer
2.明白数据是否是纯文本数据
源:假设是。Reader。否,InputStream
汇:假设是,Writer,否,OutputStream
前两点明白后院,体系也就明白了
3.明白详细的设备
源设备:
硬盘:File
键盘:System.in
内存: 数组
网络:Socket流
汇设备:
硬盘:File
控制台:System.out
内存:数组
网络:Socket流
4.是否须要其它额外功能(对上述增强)
1.是否须要高效(缓冲区),
是,就加上buffer
2.是否须要转换
字节->字符:InputStreamReader:
InputStreamReader isr = new InputStreamReader(System.in);
FileWriter fe = new FileWriter("ACM.txt");
字符->字节:OutputStreamWriter:
OutputStreamWriter osw = new OutputStreamWriter(System.out)
练习:
1.复制一个文本文件:
(1)明白源和汇:源:InputStream Reader 汇:OutputStream Writer
(2)是否是纯文本:是,源:Reader 汇:Writer
(3)明白设备:源:File 汇:File
FileReader fr = new FileReader(); FileWriter fw = new FileWriter();
(4)须要高效:BufferedReader br = new BufferedReader(new FileReader("acm.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("ACM.txt"));
2.读取键盘信息,并写入一个文件里
(1)明白源和汇:源:InputStream、 Reader 汇:OutputStream 、Writer
(2)是否是纯文本:是。源:Reader 汇:Writer
(3)明白设备:源:键盘 (System.in) 汇:硬盘 (File)
InputStream is = System.in(); FileWriter fw = new FileWriter("ACM.txt");
(4)须要功能:1.转换,由于已明白源是Reader,这样更便捷
InputStreamReader isr = new InputStreamReader(System.in);
FileWriter fe = new FileWriter("ACM.txt");
2.高效:BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new FileWriter("ACM.txt"))3.将文本文件的数据显示到控制台上
(1)明白源和汇:源:InputStream、 Reader 汇:OutputStream 、Writer
(2)是否是纯文本:是。源:Reader 汇:Writer
(3)明白设备:源:硬盘(File) 汇:控制台(System.out)
FileReader fr = new FileReader("acm.txt");
OutStream out = System.ou;
(4)须要功能:
转换:FileReader fr = new FileReader("acm.txt");
OutputStreamWriter osw = new OutputStreamWriter(System.out);
高效:BufferedReader br = new BufferedReader(new FileReader("acm.txt"))
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
4.读取键盘数据,显示到控制台
(1)明白源和汇:源:InputStream、 Reader 汇:OutputStream 、Writer
(2)是否是纯文本:是。源:Reader 汇:Writer
(3)明白设备:源:键盘 (System.in) 汇:控制台(System.out)
InputSream in = System.in
OutputStream out = System.out;
(4)须要转换,都是字节流。操作文本数据,使用字符流操作,更方便。一次读取一行
InputStreamReader isr = new InputStreamReader(System.in);
OutputStreamWriter osw = new OutputStreamWriter(System.out);
高效:BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.ou))
5.转换流的编码解码
将中文字符串数据依照指定的编码表写到文本文件里
(2)是否是纯文本:是,汇:Writer
(3)明白设备: 汇:硬盘 (File)
FilrWriter fw = new FileWriter("acm.txt");此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的
fw.write("你好");//中文占两个字节
FileWriter fw = new FileWriter("gbk.txt");
这两句代码的功能是等同的。
FileWriter:事实上就是转换流指定了本机默认码表的体现,并且这个转换流的子类对象,能够方便操作文本文件,简单说,操作文件的字节流+本机默认的编码表
假设操作文本文件须要详细的编码表,就必须使用转换流
(4)须要高效
BufferedWriter bw = new BufferedWriter(osw);
import java.io.*; public class Main { public static void main(String[] args) throws IOException { //Demo_1(); Demo_2(); Demo_3(); } public static void Demo_3() throws IOException { // TODO Auto-generated method stub //FileReader fr = new FileReader("gbk.txt"); InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk.txt"),"GBK"); char[] by = new char[20]; //int len = fr.read(by); int len = isr.read(by); String str = new String(by,0,len); System.out.println(str); // fr.close(); isr.close(); } public static void Demo_2() throws IOException { //OutputStreamWriter(OutputStream out, String charsetName) ,创建使用指定编码表。 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"),"GBK");// BufferedWriter bw = new BufferedWriter(osw); bw.write("你好"); bw.close(); } public static void Demo() throws IOException{ FileWriter fw = new FileWriter("gbk.txt"); fw.write("你好"); fw.close(); } }
注意既然需求中已经指定编码表的动作,那就不能够使用FileWriter。由于FileWriter内部使用的是默认的本地码表,仅仅能使用其父类。OuptStreamWriter,而OutputStreamWriter接收一个字节输出的对象,并且是文件操作,那么对象就应该是FileOuputStream
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("x.txt"),"GBK");
转换流什么时候用?
1.源或汇相应的设备是字节流,可是操作的是文本数据,能够是用转换做桥梁,提高文本操作的便捷
2.一旦操作文本涉及到详细的指定的编码表时,必须使用转换流