概述
IO流用来处理设备之间的数据传输
Java对数据的操作时通过流的方式
Java用于操作流的对象都在IO包中
流按操作的数据分为:字节流和字符流
流按流向不同分为:输入流和输出流
IO流常用基类
字节流的抽象基类
InputStream,OuputStream
字符流的抽象基类
Reader,Writer
注:
这些基类的子类都是以基类的名称结尾的,如FileReader,FileInputStream
字节流

字节流:以字符为导向的流
简单介绍一个例子,演示字节流对图片的复制
   1: import java.io.*;   2:     3: class IOCopyPicDemo    4: {   5:     public static void main(String[] args) throws IOException   6:     {   7:         //创建字节文件读取流   8:         FileInputStream fis = new FileInputStream("pic1.png");   9:         //创建字节文件输入流  10:         FileOutputStream fos = new FileOutputStream("pic2.png");  11:    12:         //定义缓冲区  13:         byte[] buf = new byte[1024];  14:           15:         //读取字节到缓冲区,并写入输出流,即写入到文件  16:         int len = -1;  17:         while( (len = fis.read(buf)) != -1)  18:         {  19:             fos.write(buf,0,len);  20:         }  21:           22:         //关闭资源  23:         fis.close();  24:         fos.close();  25:           26:     }  27: }字符流

字符流:以字符为导向的流
字符流只能操作文本,下面是字符流拷贝文本文件的例子
1: import java.io.*;
2:
3: class IOFileCopyDemo
   4: {5: public static void main(String[] args) throws IOException
   6:     {7: //创建字符文件输入流
   8:         FileReader fr = new FileReader("IODemo1.java");9:
10: //创建字符文件输出流
  11:         FileWriter fw = new FileWriter("IODemo2.java");12:
13: //字符缓冲区
14: char[] data = new char[1024];
15:
16: int len = -1;
17:
18: //读入字符,并写入文件
19: while( (len = fr.read(data)) != -1)
  20:         {21: fw.write(data,0,len);
22: }
23:
24: //关闭资源
25: fw.close();
26: fr.close();
27:
  28:     }  29: }转换流
InputStreamReader,OutputStreamWriter
InputStreamReader(InputStream in):接收字节流,转换为字符流
InputStreamReader(InputStream in, Charset cs):接收字节流,按照指定的编码表转换为字符流
其实FileReader就是使用系统默认编码表的转换流,其内部使用的也是字节流,因此,下面两句是一样的
FileReader fr = new FileReader(“1.txt”);
InputStreamReader isr = new InputStreamReader(new FileInputStream(“1.txt”),”GDK”);
缓冲区与装饰设计模式
缓冲区
顾名思义,对数据起到一个缓冲的作用,减少了流之间的耦合性,减少了每次与硬盘设备之间的通信,提供了效率
BufferedWriter,BufferedReader,BufferedInputStream,BufferedOutputStream实现了对对应流的缓冲(其实内部就是定义了一个数组,无论读一个数组或者一个字,内部都会事先读取好一个足够大的缓冲区数组,供你使用)
装饰设计模式
当我们对一个体系中的类进行功能加强时,如果对每个类都定义其子类,会使得整个体系显得非常臃肿,而且扩展性不好,没出现一个新类,就要对其创建一个用于功能增强的子类,整个时候就出现了装饰类,这个装饰类就是对一个体系的类都进行功能的加强,使用时传入体系中类的对象,同时这个装饰类又应该属于这个体系
例如:BufferedReader,对Reader体系中的所有类进行加强,加入缓冲技术,方法的接口中使用的是父类引用,利用多态提高了功能扩展性
装饰设计模式的演变过程

IO异常处理方式
IO的异常基类为IOException,是Exception的子类,代表需要被程序员处理
由于IO涉及到了各种资源,所以try处理时,必须要在finally块中进行关闭资源操作
1: /*
2: IO异常处理
3: */
4:
5: import java.io.*;
6:
7: class IOExceptionHandleDemo
   8: {9: public static void main(String[] args)
  10:     {11: FileWriter fw = null;
12: FileReader fr = null;
13: try
  14:         {  15:             fw = new FileWriter("IODemo1.txt");  16:             fr = new FileReader("IODemo2.txt");17: fw.write(fr.read());
18:
19: }
20: catch (IOException e)
  21:         {22: System.out.println(e.toString());
23: }
24: finally
  25:         {26: if(fw != null)
  27:             {28: try
  29:                 {  30:                     fw.close();  31:                 }  32:                 catch (IOException e)  33:                 {  34:                     System.out.println("close exception!");  35:                 }  36:             }  37:             if(fr != null)  38:             {  39:                 try  40:                 {  41:                     fr.close();  42:                 }  43:                 catch (IOException e)  44:                 {  45:                     System.out.println("close exception!");  46:                 }  47:             }  48:         }  49:     }  50: } 
 
















