本帖最后由 fengjietian 于 2015-7-31 20:13 编辑
java.io 中, BufferedReader除了多出一个 readLine()函数, 还有什么区别吗? 好像FileReader也可以把输入流读取到一个缓冲区中间。
- import java.io.*;
- public class InputOutPutDemo {
- static String srcPath = "src/InputOutPutDemo.java";
- static String destPath = "C:/testDemo.java";
- public static void main(String[] args) {
- copyFile3();
- }
-
- //BufferedReader BufferedWriter
- static void copyFile2() {
- BufferedReader br = null;
- BufferedWriter bw = null;
- try {
- br = new BufferedReader(new FileReader(srcPath));
- bw = new BufferedWriter(new FileWriter(destPath));
- char[] buf = new char[1024];
- int num = 0;
- while((num = br.read(buf)) != -1) {
- bw.write(buf, 0, num);
- bw.flush();
- }
- } catch(IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(br != null)
- br.close();
- if(bw != null)
- bw.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- //FileReader FileWriter
- static void copyFile() {
- FileReader fr = null;
- FileWriter fw = null;
-
- try {
- fr = new FileReader(srcPath);
- fw = new FileWriter(destPath);
- char[] buf = new char[10];
- int num = 0;
-
- while((num = fr.read(buf)) != -1) {
- fw.write(buf, 0, num);
- fw.flush();
- }
- } catch(FileNotFoundException e) {
- e.printStackTrace();
- } catch(IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(fr != null)
- fr.close();
- if(fw != null)
- fw.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
复制代码
就像上面的代码, FileReader不是也用了缓冲区buf吗, 为什么还要另外有一个类BufferedReader?
|
|