JAVA基础学习IO流总结<一>

IO包常用类层级关系:

java.lang.object
              |—InputStreanm字节输入流

                         |--ObjectInputStream

                         |—FileInputStream用于读取诸如图像数据之类的原始字节流

                         |--PipedInputStream
                         |--FilterInputStream
                                         |--BufferedInputStream缓冲输入流
                                         |—DataInputStream

              |—OutputStreanm字节输出流

                         |—FileOutputStream

                         |—PipedOutputStream

                         |--ObjectOutputStream

                         |--FilterOutputStream

                                         |--BufferedOutputStream缓冲输出流

                                         |--DataOutputStream

                                         |—PrintStream

             |—Reader字符读取流

                         |--BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取

                         |--CharArrayReader

                         |--FilterReader

                         |--InputStreamReader

                                        |—FileReader 用来读取字符文件的类

                         |--PipedReader

                         |—StringReader

             |--Writer字符写出流

                         |--BufferedWriter将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

                         |--CharArrayWriter

                         |--FilterWriter

                         |--OutputStreamWriter

                                       |—FileWriter 用于写入字符流的类

                         |--PipedWriter

                         |--PrintWriter

                         |—StringWriter

文本文件复制代码详解

   1:  package Itcast.com;
   2:  import java.io.*;
   3:  public class IOfileCopy 
   4:  {
   5:      public static void main(String[] args)throws IOException
   6:      {
   7:          copy();        
   8:      }    
   9:      public static void copy()throws IOException    
  10:      {
  11:          //定义文件读写流对象,并初始化
  12:          FileWriter fw=null;
  13:          FileReader fr=null;
  14:          //由于要操作底层资源,所有需要一个标准的异常处理过程
  15:          try
  16:          {            
  17:            //创建文件写出流对象,关联复制文件
  18:            fw =new FileWriter("F://1.txt");
  19:            //创建文件读取流对象。关联已有原文件
  20:            fr=new FileReader("D://qq_333.txt");
  21:            //在内存中定义一个数组作为缓冲区,提高复制效率
  22:            char[] buf= new char[1024];
  23:            //定义记录读取字符个数的变量
  24:            int len=0;    
  25:            //read()的len返回的是读了多少个字符,读到末尾则返回-1,把字符读入指定对象中。
  26:            while((len=fr.read(buf))!=-1)
  27:             {
  28:              //把指定数组里的内容,按照指定长度写出去。
  29:              fw.write(buf,0,len);        
  30:             }
  31:          }
  32:          //读写流各刷一次
  33:          catch (IOException e)
  34:          {
  35:              throw new RuntimeException("读写失败");
  36:          }        
  37:          //处理关闭流时的异常
  38:          finally
  39:          {
  40:              if(fr!=null)
  41:              {
  42:                  fw.close();
  43:              }
  44:              if(fr!=null)
  45:              {
  46:                  fw.close();
  47:              }
  48:          }
  49:      }
  50:  }

图片复制代码详解:

   1:  package Itcast.com;
   2:  import java.io.*;
   3:  /**复制图片原理:
   4:   * 用字节读取流对象和图片关联
   5:   * 用字节写入流对象创建一个图片文件来存储读到的图片
   6:   * 通过循环读写,完成数据存储
   7:   * @author shantui
   8:   */
   9:   
  10:  public class IOcopyImg
  11:  {
  12:      public static void main(String[] args)
  13:      {
  14:          //创建用于读取诸如图像数据之类的原始字节流的字节读取流对象
  15:          FileInputStream  fis=null;
  16:          //创建用于写入诸如图像数据之类的原始字节流的字节写入流对象
  17:          FileOutputStream fos=null;
  18:          try
  19:          {
  20:              //读取流关联原始图片
  21:           fis=new FileInputStream("F:\\未命名.gif");
  22:           //写入流关联复制图片,一定要指明存放复制件的文件名,不会自动创建。
  23:           fos=new FileOutputStream("D:\\1.gif");
  24:           //存放字节的缓冲数组须定义为字节数组
  25:           byte arr[]=new byte[1024];
  26:           int len=0;
  27:           //与字符流读取写入的方法相同
  28:           while((len=fis.read(arr))!=-1)
  29:           {
  30:               fos.write(arr,0,len);             
  31:           }
  32:          }
  33:          //异常处理过程也一样
  34:          catch (IOException e)
  35:          {
  36:              throw new RuntimeException("读取图片失败");
  37:          }
  38:          finally
  39:          {
  40:              try
  41:              {
  42:                  if(fis!=null)
  43:                  {
  44:                      fis.close();
  45:                  }
  46:              }
  47:              catch (IOException e)
  48:              {
  49:                  throw new RuntimeException("读图片失败");
  50:              }
  51:              try
  52:              {
  53:                  if(fos!=null)
  54:                  {
  55:                      fos.close();
  56:                  }
  57:              }
  58:              catch (IOException e)
  59:              {
  60:                  throw new RuntimeException("读取图片失败");
  61:              }
  62:          }
  63:      }        
  64:   
  65:  }

复制mp3的代码演示:

   1:  package Itcast.com;
   2:  import java.io.*;
   3:  public class IOcopyMp3
   4:  {
   5:      public static void main(String[] args)throws IOException    
   6:      {
   7:          //定义字节流读取和写入缓冲区,用于缓存从硬盘上得到的数据
   8:          BufferedInputStream  bufis=new BufferedInputStream (new FileInputStream("F:\\Listen.mp3"));
   9:          BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("D:\\yes.mp3"));
  10:          //同样用字节数组最为内存缓存
  11:          byte arr[]=new byte[1024];
  12:          int len= 0;
  13:          //同样的方法循环读取并处理异常
  14:          try
  15:          {
  16:              while((len=bufis.read(arr))!=-1)
  17:              {
  18:                  bufos.write(arr,0,len);
  19:              }
  20:          }
  21:          catch (IOException e)
  22:          {
  23:              throw new RuntimeException("复制音频失败");
  24:          }
  25:          finally
  26:          {
  27:             try
  28:             {
  29:              if(bufis!=null)
  30:              {
  31:                  bufis.close();
  32:              }
  33:             }
  34:          catch (IOException e)
  35:          {
  36:              throw new RuntimeException("读音频失败");
  37:          }
  38:          try
  39:          {
  40:              if(bufos!=null)
  41:              {
  42:                  bufos.close();
  43:              }
  44:          }
  45:          catch (IOException e)
  46:          {
  47:              throw new RuntimeException("写音频失败");
  48:          }
  49:          }
  50:          
  51:          
  52:          
  53:      }
  54:   
  55:  }


 

原文地址:https://www.cnblogs.com/94007boy/p/2684120.html