Difference between java.io.PrintWriter and java.io.BufferedWriter?

后端 未结 8 1460
南笙
南笙 2020-12-02 06:04

Please look through code below:

// A.class
File file = new File(\"blah.txt\");
FileWriter fileWriter = new FileWriter(file);
PrintWriter printWriter = new P         


        
8条回答
  •  佛祖请我去吃肉
    2020-12-02 06:59

    In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,

    Note: Text content in the code blocks is automatically word-wrapped

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

    will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.

提交回复
热议问题