Stream是按字节的,Reader Writer是按字符的
Reader
举个栗子:
1 package io; 2 3 import java.io.*; 4 public class TestFileReader { 5 public static void main(String[] args) { 6 FileReader fr = null; 7 int c = 0; 8 try { 9 fr = new FileReader("d:\share\java\io\TestFileReader.java"); 10 //int ln = 0; 11 while ((c = fr.read()) != -1) { 12 //char ch = (char) fr.read(); 13 System.out.print((char)c); 14 //if (++ln >= 100) { System.out.println(); ln = 0;} 15 } 16 fr.close(); 17 } catch (FileNotFoundException e) { 18 System.out.println("找不到指定文件"); 19 } catch (IOException e) { 20 System.out.println("文件读取错误"); 21 } 22 23 } 24 }
Writer
1 package io; 2 3 import java.io.*; 4 public class TestFileWriter { 5 public static void main(String[] args) { 6 FileWriter fw = null; 7 try { 8 fw = new FileWriter("d:/unicode.dat"); 9 for(int c=0;c<=50000;c++){ 10 fw.write(c); 11 } 12 fw.close(); 13 } catch (IOException e1) { 14 e1.printStackTrace(); 15 System.out.println("文件写入错误"); 16 System.exit(-1); 17 } 18 } 19 }
疑惑:write一个int,计算机内部到底是怎样write的呢,肯定不是写成数据,而是写成流,流是怎样写的呢,写int还是写char