标签:style color io os 使用 java strong sp 文件
转换流:
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))
未完
标签:style color io os 使用 java strong sp 文件
原文地址:http://blog.csdn.net/wjw0130/article/details/40458417