为什么要把FileWriter转换成BufferedWriter?

我知道bufferWriter具有缓冲的功能
其他的就不太清楚了
感觉把FileWriter转换成BufferedWriter代码看起来很丑

 public class Test {

    public static void main(String[] args) throws IOException {
        f1();
        f2();
    }

    static void f1() throws IOException {
        FileWriter fileWriter = new FileWriter("src/t.txt", true);
        fileWriter.write("bString");
        fileWriter.close();
    }

    static void f2() throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/t.txt", true));
        bufferedWriter.write("aString");
        bufferedWriter.close();
    }
}

如果说性能上有差异,怎么能测试出,俩者性能上存在差异?

还有这种写法,这么写的目的是什么?

  PrintWriter out  = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));

FileWriter: You were going to write to the file a number of times -- especially many short writes like a list of a thousand names or something like that
BufferedWriter:Send only large chunks of data to the FileWriter.Writing one large chunk to a file is more efficient than many small ones because each call to FileWriter.write() involves a call to the operating system, and those are slow

Below was a simple test.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class TestBuffer {

    public static void main(String[] args) throws IOException {

        long startTime = System.currentTimeMillis();
        f1();
        long stopTime = System.currentTimeMillis();
        long elapsedTime = stopTime - startTime;
        System.out.println(elapsedTime);

        startTime = System.currentTimeMillis();
        stopTime = System.currentTimeMillis();
        f2();
        elapsedTime = stopTime - startTime;
        System.out.println(elapsedTime);
    }

    static void f1() throws IOException {
        FileWriter fileWriter = new FileWriter("src/t1.txt", true);
        for (int i = 0; i < 10000000; i++) {
            fileWriter.write("bString");
        }
        fileWriter.close();
    }

    static void f2() throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/t2.txt", true));
        for (int i = 0; i < 10000000; i++) {
            bufferedWriter.write("aString");
        }
        bufferedWriter.close();
    }
}

output

639
0

What's more, BufferedWriter has newLine() method to write new line(\n or \r\n) according to OS.

1.缓冲 输入字符流出现的目的是为了提高读取文件 的效率,该类内部维护了一个字符数组.2.拓展了FileReader的功能,如;使用BUfferedReader拓展的功能,readLine() 一次读取一行文本,如果读到了文件的末尾返回null表示。

there was a small mistake.
here is the correct one.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class TestBuffer {

    public static void main(String[] args) throws IOException {

        long startTime = System.currentTimeMillis();
        f1();
        long stopTime = System.currentTimeMillis();
        long elapsedTime = stopTime - startTime;
        System.out.println(elapsedTime);

        startTime = System.currentTimeMillis();
        f2();
        stopTime = System.currentTimeMillis();
        elapsedTime = stopTime - startTime;
        System.out.println(elapsedTime);
    }

    static void f1() throws IOException {
        FileWriter fileWriter = new FileWriter("src/t1.txt", true);
        for (int i = 0; i < 10000000; i++) {
            fileWriter.write("bString");
        }
        fileWriter.close();
    }

    static void f2() throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/t2.txt", true));
        for (int i = 0; i < 10000000; i++) {
            bufferedWriter.write("aString");
        }
        bufferedWriter.close();
    }
}

output

626
375