5.19Java工具类

5.19Java工具类

原因

因为我们看到从文件拷贝到文件,从文件拷贝到字节数组,将字节数组拷贝到文件这些拷贝都是输入输出流对接,所以我们可以将这个过程封装成工具类供与调用来使用

所以可以分成三个段:

  • 封装拷贝的过程

  • 封装释放资源

  • JDK1.7以后带的释放资源

    /**
    * 对接输入输出流--->一根管道
    * @param is
    * @param os
    */
   public static void copy(InputStream is, OutputStream os) throws IOException {
       /*里面只写操作代码,把输入输出流定义为形参*/
       try {
           //分段读取
           byte[] flush = new byte[1024]; //缓冲容器
           /*一次接收的文件长度*/
           int temp = -1;
           /*进行分段读取操作*/
           while ((temp=is.read(flush))!=-1){
               //进行分段写出操作
               os.write(flush, 0, temp);
          }
           //进行文件刷新
           os.flush();
      }catch (FileNotFoundException e){
           System.out.println(e.getMessage());
           e.printStackTrace();
      }finally {
           try {
               //释放资源,分别关闭。先打开的后关闭
               if (null!=os){
                   os.close();
              }
          }catch (Exception e){
               System.out.println(e.getMessage());
               e.printStackTrace();
          }

           try {
               //释放资源,源文件资源
               if (null!=is){
                   is.close();
              }
          }catch (Exception e){
               System.out.println(e.getMessage());
               e.printStackTrace();
          }
      }
  }
封装关闭资源部分
    /**
    * 释放资源的方法--->只关闭输入输出流
    * @param is
    * @param os
    */
   public static void close(InputStream is, OutputStream os){
       /*进行释放资源的操作*/
       try {
           if (null!=os){
               os.close();
          }
      }catch (IOException e){
           e.printStackTrace();
      }

       try {
           if (null!=is){
               is.close();
          }
      }catch (IOException e){
           e.printStackTrace();
      }
  }


   /*
   再InputStream和OutputStream两个流当中实现了一个接口为Closeable
   那么可以把形参定义为这个接口的实现
   如果传入多个同类型参数使用可变参数的方法
    */
   /**
    * 多个io流的关闭--->释放资源
    * @param ios
    */
   public static void Close(Closeable... ios){
       /*使用增强for循环判断里面的对象*/
       for (Closeable io : ios){
           //进行释放资源操作
           try {
               if (null!=io){
                   io.close();
              }
          }catch (IOException e){
               System.out.println(e.getSuppressed());
               e.printStackTrace();
          }
      }
  }
JDK1.7关闭资源自带的方法
    /**
    * try...with...resource
    * @param srcPath
    * @param destPath
    */
   public static void copyNo2(String srcPath, String destPath){
       //创建源文件
       File src = new File(srcPath);
       File dest = new File(destPath);
       //选择流--->使用try...with...resource方法
       try(InputStream is = new FileInputStream(src);
       OutputStream os = new FileOutputStream(dest)) {
           //进行文件操作
           //缓冲容器
           byte[] flush = new byte[1024];
           //一次接收的长度
           int temp = -1;
           //进行文件的分段写出
           while ((temp=is.read(flush))!=-1){
               os.write(flush, 0, temp);
          }
           //刷新流
           os.flush();
      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      }
       /*
       try...with...resource会自动释放资源
        */
  }

 

 

posted @ 2021-05-19 19:33  俊king  阅读(108)  评论(0编辑  收藏  举报