thinking in java (三十) ----- IO之 FileReader和FileWriter
阿新 • • 發佈:2018-11-16
介紹
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