thinking in java (三十) ----- IO之 FileReader和FileWriter

介绍

FileReader是用于读取字符流的类,继承于InputStreamReader,如果要读取原始字节流,需要使用InputStream。

FileWriter是用于写入字符流的类,继承于OutputStreamWriter,如果要写入原始字节流,需要使用OutptuStream。

源码

FileReader

package java.io;

public class FileReader extends InputStreamReader {

    public FileReader(String fileName) throws FileNotFoundException {
        super(new FileInputStream(fileName));
    }

    public FileReader(File file) throws FileNotFoundException {
        super(new FileInputStream(file));
    }

    public FileReader(FileDescriptor fd) {
        super(new FileInputStream(fd));
    }
}

从中,我们可以看出FileReader是基于InputStreamReader实现的。 

FileWriter

package java.io;

public class FileWriter extends OutputStreamWriter {

    public FileWriter(String fileName) throws IOException {
        super(new FileOutputStream(fileName));
    }

    public FileWriter(String fileName, boolean append) throws IOException {
        super(new FileOutputStream(fileName, append));
    }

    public FileWriter(File file) throws IOException {
        super(new FileOutputStream(file));
    }

    public FileWriter(File file, boolean append) throws IOException {
        super(new FileOutputStream(file, append));
    }

    public FileWriter(FileDescriptor fd) {
        super(new FileOutputStream(fd));
    }
}

运行结果
c1=字
buf=流示例0123456

原文:http://www.cnblogs.com/skywang12345/p/io_22.html

猜你喜欢

转载自blog.csdn.net/sinat_38430122/article/details/84023871