SCJP解析系列之七

 

  作者:刘颖

    试题:
    When comparing java.io.BufferedWriter to java.io.FileWriter,which capability exist as a method in only one of the two?
    A.closing the stream
    B.flushing the stream
    C.writing to the stream
    D.marking a location in the stream
    E.writing a line separator to the stream

    这个题目就是看下面的几个方面只在其中一个类中存在,我们还是一起来看一下API吧,我最近特别依赖它.java.io.BufferedWriter有以下几个方法:
    close(): 关闭此流,但要先刷新它。
    flush(): 刷新该流的缓冲。
    void newLine(): 写入一个行分隔符。
    write(char[] cbuf, int off, int len):写入字符数组的某一部分。
    write(int c):写入单个字符。
    write(String s, int off, int len):写入字符串的某一部分.
java.io.FileWriter有从java.io.OutputStreamWriter继承的方法:
    close();
    flush();
    write(int c);
    write(char[] cbuf,int off,int len);
    write(String str,int off,int len);

下面我们来一起看一下选项吧:
    A.关闭流的方法,两者都有的
    B.刷新流的方法,二者也皆有
    C.向流中写东西.当然是都可以写东西的啦
    D.两者都没有提供在流中定位的方法
    E.向流中写入一个行分隔符,这个在java.io.BufferedWriter类中有提供这个功能,即是void newLine():写入一个行分隔符。
    答案为:E