java学习--IO流(2)

来源:互联网 发布:美国经济数据 编辑:程序博客网 时间:2024/06/05 06:19

01)BufferWriter

[java] view plaincopy
  1. /* 
  2.  * BufferWriter: 
  3.  * 为了提高字符写入流效率,加入了缓冲技术。 
  4.  * 只需要将要被提高效率的流对象作为参数传递给缓冲区的构造函数即可。 
  5.  * newLine():写入一个行分隔符。行分隔符字符串由系统属性 line.separator 定义,并且不一定是单个新行 ('\n') 符。 
  6.  */  
  7. public class BufferWriterDemo {  
  8.     public static void main(String[] args) {  
  9.         method();  
  10.     }  
  11.     public static void method(){  
  12.         FileWriter fw = null;//创建一个引用。  
  13.         BufferedWriter bufw = null;  
  14.         try{  
  15.             fw = new FileWriter("buf.txt");//初始化流对象。  
  16.             //为了提高字符写入流效率,加入了缓冲技术。  
  17.             //只需要将要被提高效率的流对象作为参数传递给缓冲区的构造函数即可。  
  18.             bufw = new BufferedWriter(fw);  
  19.             for (int i = 1; i <= 5; i++){  
  20.                 bufw.write(i + "   BufferWriter");  
  21.                 bufw.newLine();//写入一个行分隔符。  
  22.             }  
  23.         }catch(IOException e){  
  24.             throw new RuntimeException("读取失败");  
  25.         }finally{  
  26.             try {  
  27.                 if(bufw != null)  
  28.                     //记住:只要用掉缓冲区,就要记得使用刷新。  
  29.                     bufw.close();//其实关闭缓冲区,就是在关闭缓冲区中的流对象。fw.close()就不需要再关闭。  
  30.             } catch (IOException e) {  
  31.                 throw new RuntimeException("读取关闭失败");  
  32.             }  
  33.         }  
  34.     }  
  35. }  
运行结果如下图所示:


02)BufferedReader

[java] view plaincopy
  1. /* 
  2.  * 字符读取流缓冲区: 
  3.  * 该缓冲区提供了一个一次读一行的方法。(readLine()),方法于对文本数据的获取。 
  4.  * 当返回null时,表示读到文件末尾。 
  5.  */  
  6. public class BufferedReaderDemo {  
  7.     public static void sop(Object obj){  
  8.         System.out.println(obj);  
  9.     }  
  10.     public static void main(String[] args) {  
  11.         method();  
  12.     }  
  13.     public static void method(){  
  14.         FileReader fr = null;  
  15.         BufferedReader bufr = null;  
  16.         try{  
  17.             fr = new FileReader("buf.txt");//初始化读取流对象。  
  18.             bufr = new BufferedReader(fr);//将字符流读取对象作为参数传递给缓冲对象的构造函数。  
  19.             String line = null;  
  20.             while((line = bufr.readLine()) != null)  
  21.                 sop("(readLine)" + line);  
  22.         }catch(IOException e){  
  23.             throw new RuntimeException("写入失败");  
  24.         }finally{  
  25.             try{  
  26.                 if (bufr != null)  
  27.                     bufr.close();  
  28.             }catch(IOException e){  
  29.                 throw new RuntimeException("写入关闭失败");  
  30.             }  
  31.         }  
  32.     }  
  33. }  
运行结果如下图所示:


03)通过缓冲区复制文本文件

[java] view plaincopy
  1. /* 
  2.  * 通过通过缓冲区复制文本文件: 
  3.  * BufferedReader。 
  4.  * BufferedWriter。 
  5.  */  
  6. public class ReadLineDemo_copy {  
  7.     public static void sop(Object obj){  
  8.         System.out.println(obj);  
  9.     }  
  10.     public static void main(String[] args) {  
  11.         method();  
  12.     }  
  13.     public static void method(){  
  14.         BufferedReader bufr = null;  
  15.         BufferedWriter bufw = null;  
  16.         try{  
  17.             bufr = new BufferedReader(new FileReader("RuntimeDemo.java"));  
  18.             bufw = new BufferedWriter(new FileWriter("BufferedWriter_copy.txt"));  
  19.             String line = null;//相当于中转站。  
  20.             while((line = bufr.readLine()) != null){  
  21.                 bufw.write(line);  
  22.                 bufw.newLine();//换行。  
  23.             }  
  24.         }catch(IOException e){  
  25.             throw new RuntimeException("读取失败");  
  26.         }finally{  
  27.             try {  
  28.                 if (bufr != null)  
  29.                     bufr.close();  
  30.             } catch (IOException e) {  
  31.                 throw new RuntimeException("读取关闭失败");  
  32.             }  
  33.             try {  
  34.                 if (bufw != null)  
  35.                     bufw.close();  
  36.             } catch (IOException e) {  
  37.                 sop("写入关闭失败");  
  38.             }  
  39.         }  
  40.     }  
  41. }  
运行结果如下图所示:


05)模拟BufferedReader

[java] view plaincopy
  1. /* 
  2.  * 模拟BufferedReader。 
  3.  * 通过自定义一个类中包含一个功能和readLine一致的方法。 
  4.  */  
  5. public class MyBufferedReaderDemo {  
  6.     public static void main(String[] args) {  
  7.         method();  
  8.     }  
  9.       
  10.     public static void method(){  
  11.         MyBufferedReader mybufr = null;  
  12.         try{  
  13.             mybufr = new MyBufferedReader(new FileReader("buf.txt"));  
  14.             String line = null;  
  15.             while((line = mybufr.myReadLine()) != null)  
  16.                 System.out.println(line);  
  17.         }catch(IOException e){  
  18.             e.printStackTrace();  
  19.         }finally{  
  20.             if (mybufr != null)  
  21.                 try{  
  22.                     mybufr.myClose();  
  23.                 }catch(IOException e){  
  24.                     e.printStackTrace();  
  25.                 }  
  26.         }  
  27.     }  
  28. }  
  29. class MyBufferedReader{  
  30.     private Reader r;  
  31.     MyBufferedReader(Reader r){  
  32.         this.r = r;  
  33.     }  
  34.     public String myReadLine() throws IOException{//可以一次读一行数据的方法。  
  35.         //定义一个临时容器(StringBuilder)。  
  36.         StringBuilder sb = new StringBuilder();  
  37.         int ch = 0;  
  38.         while ((ch = r.read()) != -1){  
  39.             if (ch == '\r')  
  40.                 continue;  
  41.             if (ch == '\n')  
  42.                 return sb.toString();  
  43.             else  
  44.                 sb.append((char)ch);  
  45.         }  
  46.         if (sb.length() != 0)  
  47.             return sb.toString();  
  48.         return null;  
  49.     }  
  50.     public void myClose() throws IOException{  
  51.         r.close();  
  52.     }  
  53. }  
运行结果如下图所示:


06)装饰设计模式

[java] view plaincopy
  1. /* 
  2.  * 装饰设计模式: 
  3.  * 当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有对象的功能,并提供加强功能 
  4.  * 那么自定义的该类称为装饰类。 
  5.  *  
  6.  * 特点:装饰类通常会通过构造方法接受被装饰的对象。并基于被装饰对象的功能,提供更强的功能。 
  7.  */  
  8. public class PersonDemo {  
  9.     public static void main(String[] args) {  
  10.         method();  
  11.     }  
  12.     public static void method(){  
  13.         SuperPerson sp = new SuperPerson(new Person());  
  14.         sp.superchifan();  
  15.     }  
  16. }  
  17.   
  18. class Person{  
  19.     public void chifan(){//需要被增强的功能。  
  20.         System.out.println("吃饭!");  
  21.     }  
  22. }  
  23.   
  24. class SuperPerson{//装饰类。  
  25.     private Person p;  
  26.     SuperPerson(Person p){  
  27.         this.p = p;  
  28.     }  
  29.     public void superchifan(){//增强功能。  
  30.         sop("开胃酒!");  
  31.         p.chifan();  
  32.         sop("甜点!");  
  33.         sop("来一把!");  
  34.     }  
  35.     public void sop(Object obj){  
  36.         System.out.println(obj);  
  37.     }  
  38. }  
运行结果如下图所示:


07)装饰和继承的区别

[java] view plaincopy
  1. /* 
  2.  * 装饰模式比继承要灵活。避免了继承体系臃肿。 
  3.  * 而且降低了类于类之间的关系。 
  4.  *  
  5.  * 装饰类因为增强已有对象,具备的功能和已有的是相同的,只不过提供了更强功能。 
  6.  * 所以装饰类和被装饰类通常是都属于一个体系中的。 
  7.  */  
  8. /* 
  9.  * MyReader//专门用于读取数据的类。 
  10.  *  |--MyTextReader 
  11.  *      |--MyBufferTextReader 
  12.  *  |--MyMediaReader 
  13.  *      |--MyBufferMediaReader 
  14.  *  |--MyDataReader 
  15.  *      |--MyBufferDataReader 
  16.  *  
  17.  * class MyBufferReader{ 
  18.  *      MyBufferReader(MyTextReader text){ 
  19.  *      } 
  20.  *      MyBufferReader(MyMediaReader media){ 
  21.  *      } 
  22.  * } 
  23.  * 上面这个类扩展性很差。 
  24.  * 找到其参数的共同类型。通过多态的形式。可以提高扩展性。 
  25.  *  
  26.  * class MyBufferReader extends MyReader{ 
  27.  *      private MyReader r; 
  28.  *      MyBufferReader(MyReader r){ 
  29.  *      } 
  30.  * }     
  31.  *  
  32.  * MyReader//专门用于读取数据的类。 
  33.  *  |--MyTextReader 
  34.  *  |--MyMediaReader 
  35.  *  |--MyDataReader 
  36.  *  |--MyBufferReader 
  37.  *  
  38.  * 以前是通过继承将每一个子类都具备缓冲功能。 
  39.  * 那么继承体系会复杂,并不利于扩展。 
  40.  *  
  41.  * 现在优化思想。单独描述一下缓冲内容。 
  42.  * 将需要被缓冲的对象。传递进来。也就是,谁需要被缓冲,谁就作为参数传递给缓冲区。 
  43.  * 这样继承体系就变得很简单。优化了体系结构。 
  44.  *  
  45.  * 装饰模式比继承要灵活。避免了继承体系臃肿。 
  46.  * 而且降低了类于类之间的关系。 
  47.  *  
  48.  * 装饰类因为增强已有对象,具备的功能和已有的是相同的,只不过提供了更强功能。 
  49.  * 所以装饰类和被装饰类通常是都属于一个体系中的。 
  50.  */  

08)自定义装饰类

[java] view plaincopy
  1. /* 
  2.  * 自定义装饰类继承Reader类,就需要实现Reader类的所有抽象方法。 
  3.  * close()与read(char[] cbuf, int off, int len)方法。 
  4.  */  
  5. class MyBufferedReader_2 extends Reader{//继承Reader类,所以需要实现Reader类的抽象方法。  
  6.     private Reader r;  
  7.     MyBufferedReader_2(Reader r){  
  8.         this.r = r;  
  9.     }  
  10.     public String myReadLine() throws IOException{//可以一次读一行数据的方法。  
  11.         //定义一个临时容器(StringBuilder)。  
  12.         StringBuilder sb = new StringBuilder();  
  13.         int ch = 0;  
  14.         while ((ch = r.read()) != -1){  
  15.             if (ch == '\r')  
  16.                 continue;  
  17.             if (ch == '\n')  
  18.                 return sb.toString();  
  19.             else  
  20.                 sb.append((char)ch);  
  21.         }  
  22.         if (sb.length() != 0)  
  23.             return sb.toString();  
  24.         return null;  
  25.     }  
  26.     public void close() throws IOException{//实现抽象类。  
  27.         r.close();  
  28.     }  
  29.     public int read(char[] cbuf, int off, int len) throws IOException{//实现抽象类。  
  30.         return r.read(cbuf, off, len);  
  31.     }  
  32.     public void myClose() throws IOException{  
  33.         r.close();  
  34.     }  
  35. }  

09)LineNumberReader

[java] view plaincopy
  1. /* 
  2.  * LineNumberReader(Reader in):使用默认输入缓冲区的大小创建新的行编号 reader。  
  3.  * int getLineNumber():获得当前行号。 
  4.  * void setLineNumber(int lineNumber):设置当前行号。 
  5.  */  
  6. public class LineNumberReaderDemo {  
  7.     public static void main(String[] args){  
  8.         method();  
  9.     }  
  10.     public static void method(){  
  11.         LineNumberReader lnr = null;  
  12.         try{  
  13.             lnr = new LineNumberReader(new FileReader("RuntimeDemo.java"));  
  14.             String line = null;  
  15.             lnr.setLineNumber(30);//设置起始标记值。  
  16.             while ((line = lnr.readLine()) != null)  
  17.                 System.out.println(lnr.getLineNumber() + ":" + line);  
  18.         }catch(IOException e){  
  19.             throw new RuntimeException("系统找不到指定的文件");  
  20.         }finally{  
  21.             if (lnr != null)  
  22.                 try{  
  23.                     lnr.close();  
  24.                 }catch(IOException e){  
  25.                 }  
  26.         }  
  27.     }  
  28. }  
运行结果如下图所示:


10)MyLineNumberReader

[java] view plaincopy
  1. /* 
  2.  * 自定义装饰类。 
  3.  * MyLineNumberReader类通过继承MyBufferedReader类以优化代码。 
  4.  */  
  5. public class MyLineNumberReaderDemo {  
  6.     public static void main(String[] args) {  
  7.         method();  
  8.     }  
  9.     public static void method(){  
  10.         LineNumberReader lnr = null;  
  11.         try{  
  12.             lnr = new LineNumberReader(new FileReader("RuntimeDemo.java"));  
  13.             String line = null;  
  14.             lnr.setLineNumber(10);//设置起始标记值。从(10+1)开始。  
  15.             while ((line = lnr.readLine()) != null)  
  16.                 System.out.println(lnr.getLineNumber() + ":" + line);  
  17.         }catch(IOException e){  
  18.             throw new RuntimeException("系统找不到指定的文件");  
  19.         }finally{  
  20.             if (lnr != null)  
  21.                 try{  
  22.                     lnr.close();  
  23.                 }catch(IOException e){  
  24.                 }  
  25.         }  
  26.     }  
  27. }  
  28.   
  29. class MyLineNumberReader extends MyBufferedReader{  
  30.     private int lineNumber;  
  31.     MyLineNumberReader(Reader r){  
  32.         super(r);  
  33.     }  
  34.     public String myReadLine() throws IOException{  
  35.         lineNumber++;  
  36.         return super.myReadLine();  
  37.     }  
  38.     public void setLineNumber(int lineNumber){  
  39.         this.lineNumber = lineNumber;  
  40.     }  
  41.     public int getLineNumber(){  
  42.         return lineNumber;  
  43.     }  
  44. }  
运行结果如下图所示:


11)字节流File读写操作

[java] view plaincopy
  1. /* 
  2.  * 字节流: 
  3.  * 读:InputStream  
  4.  * 写:Output Stream 
  5.  * void write(byte[] b):  
  6.  *  
  7.  * 需求:操作图片数据。 
  8.  * 这时就需要用到字节流。 
  9.  */  
  10. public class FileStreamDemo {  
  11.     public static void main(String[] args) throws IOException{  
  12.         writerFile();  
  13.         readFile();  
  14.     }  
  15.     public static void readFile() throws IOException{  
  16.         FileInputStream fis = new FileInputStream("fos.txt");  
  17.         //注意:只有当读取的文件小于JVM提供的内存时,才使用下面这种方法。  
  18.         //当读取文件足够大时,会发生缓冲区溢出。  
  19.         byte[] byf = new byte[fis.available()];//定义一个刚刚好的字节数组。  
  20.         fis.read(byf);  
  21.         System.out.println(new String(byf));  
  22.         fis.close();  
  23.     }  
  24.     public static void writerFile(){  
  25.         FileOutputStream fos = null;  
  26.         try{  
  27.             fos = new FileOutputStream("fos.txt");  
  28.             fos.write("abcdefg".getBytes());  
  29.         }catch(IOException e){  
  30.               
  31.         }finally{  
  32.             if (fos != null)  
  33.                 try{  
  34.                     fos.close();  
  35.                 }catch(IOException e){  
  36.                 }  
  37.         }  
  38.     }  
  39. }  

12)拷贝图片

[java] view plaincopy
  1. /* 
  2.  * 拷贝一张图片。 
  3.  * 思路: 
  4.  * 1:用字节读取流对象和图片关联。 
  5.  * 2:用字节写入流对象创建一个图片文件。用于存储获取到的图片数据。 
  6.  * 3:通过循环读写,完成数据的存储。 
  7.  * 4:关闭资源。 
  8.  */  
  9. public class CopyJpgDemo {  
  10.     public static void main(String[] args) {  
  11.         method();  
  12.     }  
  13.     public static void method(){  
  14.         FileOutputStream fos = null;  
  15.         FileInputStream fis = null;  
  16.         try{  
  17.             fos = new FileOutputStream("G:\\copy\\Java学习路线图.jpg");//目标图片文件。  
  18.             fis = new FileInputStream("G:\\copy\\Java学习.jpg");//源图片文件。  
  19.             byte[] buf = new byte[1024];  
  20.             int len = 0;  
  21.             while((len = fis.read(buf)) != -1)  
  22.                 fos.write(buf, 0, len);  
  23.         }catch(IOException e){  
  24.             throw new RuntimeException("复制文件失败");  
  25.         }finally{  
  26.             try{  
  27.                 if (fis != null)  
  28.                     fis.close();  
  29.             }catch(IOException e){  
  30.                 throw new RuntimeException("读取关闭失败");  
  31.             }  
  32.             try{  
  33.                 if (fos != null)  
  34.                     fos.close();  
  35.             }catch(IOException e){  
  36.                 throw new RuntimeException("写入关闭失败");  
  37.             }  
  38.         }  
  39.     }  
  40. }  

13)MP3的拷贝

[java] view plaincopy
  1. /* 
  2.  * mp3的复制。通过缓冲区。 
  3.  * BufferedOutputStream 
  4.  * BufferedInputStream 
  5.  */  
  6. public class CopyMp3Demo {  
  7.     public static void main(String[] args) throws IOException{  
  8.         long start = System.currentTimeMillis();  
  9.         method();  
  10.         long end = System.currentTimeMillis();  
  11.         System.out.println("time = " + (end - start) + "毫秒");//看下拷贝这个mp3文件用时多久。  
  12.     }  
  13.     //通过字节流的缓冲区完成复制。  
  14.     public static void method() throws IOException{  
  15.         BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("G:\\copy\\一眼万年.mp3"));//源mp3文件。  
  16.         BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("G:\\copy\\二眼亿年.mp3"));//目标mp3文件。  
  17.         int by = 0;  
  18.         while((by = bufis.read()) != -1)  
  19.             bufos.write(by);  
  20.         bufos.close();  
  21.         bufis.close();  
  22.     }  
  23. }  
补充:自定义字节流缓冲区
import java.io.*;class MyBufferedInputStream{private InputStream in;private byte[] buf = new byte[1024*4];private int pos = 0,count = 0;MyBufferedInputStream(InputStream in){this.in = in;}//一次读一个字节,从缓冲区(字节数组)获取。public int myRead()throws IOException{//通过in对象读取硬盘上数据,并存储buf中。if(count==0){count = in.read(buf);if(count<0)return -1;pos = 0;byte b = buf[pos];count--;pos++;return b&255;}else if(count>0){byte b = buf[pos];count--;pos++;return b&0xff;}return -1;}public void myClose()throws IOException{in.close();}}/*11111111-111111110000000000101001001010100101010010101001010byte: -1  --->  int : -1;00000000 00000000 00000000 11111111  25511111111 11111111 11111111 1111111111111111  -->提升了一个int类型 那不还是-1吗?是-1的原因是因为在8个1前面补的是1导致的。那么我只要在前面补0,即可以保留原字节数据不变,又可以避免-1的出现。怎么补0呢? 11111111 11111111 11111111 11111111                        &00000000 00000000 00000000 11111111 ------------------------------------ 00000000 00000000 00000000 11111111 0000-00011111-11100000000011111-1111  -1结论:字节流的读一个字节的read方法为什么返回值类型不是byte,而是int。因为有可能会读到连续8个二进制1的情况,8个二进制1对应的十进制是-1.那么就会数据还没有读完,就结束的情况。因为我们判断读取结束是通过结尾标记-1来确定的。所以,为了避免这种情况将读到的字节进行int类型的提升。并在保留原字节数据的情况前面了补了24个0,变成了int类型的数值。而在写入数据时,只写该int类型数据的最低8位。*/



15)读取键盘录入

[java] view plaincopy
  1. /* 
  2.  * 读取键盘录入。 
  3.  * System.out:标准的输出设备,控制台。 
  4.  * System.in:标准的输入设备,键盘。 
  5.  *  
  6.  * 需求:通过键盘录入数据: 
  7.  * 当录入一行数据后,就将该行数据打印(大写)。 
  8.  * 如果录入的数据是over,那么就停止录入。 
  9.  */  
  10. public class ReadInDemo {  
  11.     public static void sop(Object obj){  
  12.         System.out.println(obj);  
  13.     }  
  14.     public static void main(String[] args) throws IOException{  
  15.         method();  
  16.     }  
  17.     public static void method() throws IOException{  
  18.         InputStream in = System.in;  
  19.         StringBuilder sb = new StringBuilder();  
  20.         while(true){  
  21.             int ch = in.read();  
  22.             if (ch == '\r')  
  23.                 continue;  
  24.             if (ch == '\n'){  
  25.                 String s = sb.toString();  
  26.                 if ("over".equals(s)){  
  27.                     sop("结束了");  
  28.                     break;  
  29.                 }  
  30.                 sop(s.toUpperCase());  
  31.                 sb.delete(0, sb.length());  
  32.             }else  
  33.                 sb.append((char)ch);  
  34.         }  
  35.     }  
  36. }  

16)读取转换流

[java] view plaincopy
  1. /* 
  2.  * 使用readLine方法来完成键盘录入一行数据的读取。 
  3.  * readLine是(字符流)BufferedReader类中的方法。 
  4.  * 而键盘录入read是(字节流)InputStream的方法。 
  5.  */  
  6. public class TransStreamDemo {  
  7.     public static void sop(Object obj){  
  8.         System.out.println(obj);  
  9.     }  
  10.     public static void main(String[] args) throws IOException{  
  11.         method();  
  12.     }  
  13.     public static void method() throws IOException{  
  14.         //获取键盘录入对象。  
  15.         InputStream in = System.in;  
  16.         //将字节流对象转换成字符流对象,使用转换流。  
  17.         InputStreamReader isr = new InputStreamReader(in);  
  18.         //为了提高效率,将字符串进行缓冲区技术高效操作。  
  19.         BufferedReader bufr = new BufferedReader(isr);  
  20.         String line = null;  
  21.         while((line = bufr.readLine()) != null){  
  22.             if ("over".equals(line)){  
  23.                 sop("结束了!");  
  24.                 break;  
  25.             }  
  26.             sop(line.toUpperCase());  
  27.         }  
  28.         in.close();  
  29.     }  
  30. }  

17)写入流转换

[java] view plaincopy
  1. /* 
  2.  * 键盘最常见写法。 
  3.  * BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); 
  4.  */  
  5. public class TransStreamDemo_2 {  
  6.     public static void main(String[] args) throws IOException{  
  7.         method();  
  8.     }  
  9.     public static void method() throws IOException{  
  10.         //键盘最常见写法。  
  11.         BufferedReader bufr =   
  12.                 new BufferedReader(new InputStreamReader(System.in));  
  13.         BufferedWriter bufw =   
  14.                 new BufferedWriter(new OutputStreamWriter(System.out));  
  15.           
  16.         String line = null;  
  17.         while ((line = bufr.readLine()) != null){  
  18.             if ("over".equals(line)){  
  19.                 System.out.println("结束了");  
  20.                 break;  
  21.             }  
  22.             bufw.write(line.toUpperCase());  
  23.             bufw.newLine();  
  24.             bufw.flush();  
  25.         }  
  26.         bufr.close();  
  27.     }  
  28. }  

18)流操作规律

[java] view plaincopy
  1. /* 
  2.  * 1:需求:将键盘录入的数据打印到控制台。 
  3.  * 源:键盘录入。(System.in) 
  4.  * 目的:控制台。(System.out) 
  5.  *  
  6.  * 2:需求:将键盘录入的数据存储到文件中。 
  7.  * 源:键盘。(System.out) 
  8.  * 目的:文件。(FileOutputStream("文件名")) 
  9.  *  
  10.  * 3:需求:将文件中的数据打印到控制台。 
  11.  * 源:文件。(FileInputStream("文件名")) 
  12.  * 目的:控制台。(System.out) 
  13.  *  
  14.  * 流操作的基本规律:(非常重要) 
  15.  * 疑惑:流对象有很多,但不知道该用哪一个。 
  16.  * 通过三个明确: 
  17.  * 1:明确(源)和(目的)。 
  18.  *      源:输入流。(InputStream)(Reader) 
  19.  *      目的:流出流。(OutputStream)(Writer) 
  20.  * 2:明确操作的数据是否是(纯文本)。 
  21.  *      是:字符流。 
  22.  *      否:字节流。 
  23.  * 3:当体系明确后,再明确要使用哪个具体的对象。 
  24.  *      通过设备来进行区分: 
  25.  *          源设备:内存,硬盘(文件),键盘。(FileReader) 
  26.  *          目的设备:内存,硬盘(文件),控制台。(FileWriter) 
  27.  *  
  28.  * 提高效率: 
  29.  *  源:(BufferedReader) 
  30.  *  目的:(BufferedWriter) 
  31.  *   
  32.  */ 
0 0