FileReader 是用於讀取字符流的類,它繼承於InputStreamReader。要讀取原始字節流,請考慮使用 FileInputStream。
FileWriter 是用於寫入字符流的類,它繼承於OutputStreamWriter。要寫入原始字節流,請考慮使用 FileOutputStream。html
轉載請註明出處:http://www.cnblogs.com/skywang12345/p/io_22.htmljava
更多內容請參考:java io系列01之 "目錄"app
FileReader和FileWriter源碼分析ide
1. FileReader 源碼(基於jdk1.7.40)函數
1 package java.io; 2 3 public class FileReader extends InputStreamReader { 4 5 public FileReader(String fileName) throws FileNotFoundException { 6 super(new FileInputStream(fil java io系列21之 InputStreamReader和OutputStreamWritereName)); 7 } 8 9 public FileReader(File file) throws FileNotFoundException { 10 super(new FileInputStream(file)); 11 } 12 13 public FileReader(FileDescriptor fd) { 14 super(new FileInputStream(fd)); 15 } 16 }
從中,咱們能夠看出FileReader是基於InputStreamReader實現的。 源碼分析
2. FileWriter 源碼(基於jdk1.7.40)測試
1 package java.io; 2 3 public class FileWriter extends OutputStreamWriter { 4 5 public FileWriter(String fileName) throws IOException { 6 super(new FileOutputStream(fileName)); 7 } 8 9 public FileWriter(String fileName, boolean append) throws IOException { 10 super(new FileOutputStream(fileName, append)); 11 } 12 13 public FileWriter(File file) throws IOException { 14 super(new FileOutputStream(file)); 15 } 16 17 public FileWriter(File file, boolean append) throws IOException { 18 super(new FileOutputStream(file, append)); 19 } 20 21 public FileWriter(FileDescriptor fd) { 22 super(new FileOutputStream(fd)); 23 } 24 }
從中,咱們能夠看出FileWriter是基於OutputStreamWriter實現的。spa
示例程序code
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.FileWriter;; 5 import java.io.FileReader; 6 import java.io.IOException; 7 8 /** 9 * FileReader 和 FileWriter 測試程序 10 * 11 * @author skywang 12 */ 13 public class FileReaderWriterTest { 14 15 private static final String FileName = "file.txt"; 16 private static final String CharsetName = "utf-8"; 17 18 public static void main(String[] args) { 19 testWrite(); 20 testRead(); 21 } 22 23 /** 24 * OutputStreamWriter 演示函數 25 * 26 */ 27 private static void testWrite() { 28 try { 29 // 建立文件「file.txt」對應File對象 30 File file = new File(FileName); 31 // 建立FileOutputStream對應FileWriter:將字節流轉換爲字符流,即寫入out1的數據會自動由字節轉換爲字符。 32 FileWriter out1 = new FileWriter(file); 33 // 寫入10個漢字 34 out1.write("字節流轉爲字符流示例"); 35 // 向「文件中」寫入"0123456789"+換行符 36 out1.write("0123456789\n"); 37 38 out1.close(); 39 40 } catch(IOException e) { 41 e.printStackTrace(); 42 } 43 } 44 45 /** 46 * InputStreamReader 演示程序 47 */ 48 private static void testRead() { 49 try { 50 // 方法1:新建FileInputStream對象 51 // 新建文件「file.txt」對應File對象 52 File file = new File(FileName); 53 FileReader in1 = new FileReader(file); 54 55 // 測試read(),從中讀取一個字符 56 char c1 = (char)in1.read(); 57 System.out.println("c1="+c1); 58 59 // 測試skip(long byteCount),跳過4個字符 60 in1.skip(6); 61 62 // 測試read(char[] cbuf, int off, int len) 63 char[] buf = new char[10]; 64 in1.read(buf, 0, buf.length); 65 System.out.println("buf="+(new String(buf))); 66 67 in1.close(); 68 } catch(IOException e) { 69 e.printStackTrace(); 70 } 71 } 72 }
運行結果:
c1=字
buf=流示例0123456htm