当前位置:编程学习 > JAVA >>

Writer本身就有数组缓存了,那BufferedWriter有什么用呢?

本人新手 刚学到IO,有个小问题想请教各位高手~!

Writer本身有个方法void    write(char[] cbuf)  这里面的字符数组不就是缓存作用的吗?
而我粗略看了下BufferedWriter的源代码,BufferedWriter好像也就是用数组作为缓存的。
感觉BufferedWriter好像有点多余,怎么说BufferedWriter是提高效率呢? --------------------编程问答-------------------- 那个是要输出的数据吧
缓存也不一定就提高效率吧,是为了减少读写次数 --------------------编程问答-------------------- “Writer本身有个方法void    write(char[] cbuf)  这里面的字符数组不就是缓存作用的吗?”
LZ确定?
--------------------编程问答-------------------- 你没搞清Writer和BufferedWriter的关系。 --------------------编程问答--------------------
引用 2 楼 ForestDB 的回复:
“Writer本身有个方法void    write(char[] cbuf)  这里面的字符数组不就是缓存作用的吗?”
LZ确定?

是的,我是查Java的API看到的 --------------------编程问答--------------------
引用 3 楼 fangmingshijie 的回复:
你没搞清Writer和BufferedWriter的关系。


应该Writer是 BufferedWriter的基类, BufferedWriter是增强Writer的装饰类吧。
但问题是,我不感觉BufferedWriter有哪里增强的地方。。。
API上说的是有缓存,提高效率,但是Writer本身就有了。 --------------------编程问答--------------------
引用 1 楼 eastoneking 的回复:
那个是要输出的数据吧
缓存也不一定就提高效率吧,是为了减少读写次数


减少读写次数就是提高效率了。。。
而且Java API上就是说提高效率的,否则 BufferedWriter就没用了,他的作用就是提高Writer效率。 --------------------编程问答-------------------- write

public void write(char[] cbuf)
           throws IOException
Writes an array of characters.
Parameters:
cbuf - Array of characters to be written
Throws:
IOException - If an I/O error occurs

恕我愚钝,还是没有看到cbuf是buffer的意思。
--------------------编程问答-------------------- 理解一下吧。 另外BufferedWriter里面定义的方法就是比Writer
多出来的方法。多出来的方法当然是有用的。如果没用那就和Writer没什么区别了http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html


public class BufferedWriter
extends Writer

Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.

A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly.

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,

 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.

Since:
    JDK1.1
See Also:
    PrintWriter, FileWriter, OutputStreamWriter --------------------编程问答--------------------
引用 8 楼 huxiweng 的回复:
理解一下吧。 另外BufferedWriter里面定义的方法就是比Writer
多出来的方法。多出来的方法当然是有用的。如果没用那就和Writer没什么区别了http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html


public class BufferedWriter
extends Writer

Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.

A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly.

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,

 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.

Since:
    JDK1.1
See Also:
    PrintWriter, FileWriter, OutputStreamWriter


好吧,我仔细看看。
其实我不是说这两个真的没用,我知道他们肯定有实质上的区别,只是不知道在哪里。
好像语气不合适,冒犯了两位大哥,不好意思了
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,