zoukankan      html  css  js  c++  java
  • 文件拷贝的实现

    文件拷贝相当于:
    读取源文件-->写入目的地
     
     
    结合了读取文件跟写入文件
    所以,拷贝文件的步骤如下:
    1. 建立联系:建立程序与源文件、目的的的联系。
    2. 选择流:选择了两个流,输入跟输出。如:FileInputStream/FileOutputStream
    3. 操作:对源文件进行读取,对目的地进行写入
       如:程序中的一小段代码、
                            while(-1!=(len=输入流.read(byte[] b))){
                                    输出流.write(b,0,len);
                             } 
                             输出流.flush();
                            //关闭流....省略
    4. 释放:关闭流;
     
    源代码:
    未封装前:
    //建立联系
     1 File src=new File("f:/a.txt");
     2         File dest=new File("f:/b.txt");
     3         //选择流
     4         InputStream is=null;
     5         OutputStream os=null;
     6         try {
     7             is=new FileInputStream(src);
     8             os=new FileOutputStream(dest);
     9             //操作
    10             byte[] b=new byte[1024];
    11             int len=0;//实际读取长度
    12             while(-1!=(len=is.read(b))){
    13                 os.write(b, 0, len);//建议使用这个write(byte[] b, int off ,int len);
    14                 /*因为,有可能在最后一次读取的时候,b数组的值是没有装满的(1024个),所以建议使用这个方法。避免返
    15                   回一些多余的空格、*/
    16             }
    17             os.flush();//强制刷新
    18         } catch (FileNotFoundException e) {
    19             System.out.println("文件找不到");
    20         } catch (IOException e) {
    21             System.out.println("读取失败");
    22             e.printStackTrace();
    23         }finally{
    24             try {
    25                 if(null!=is){
    26                     is.close();
    27                 }
    28                 if(null!=os){
    29                     os.close();
    30                 }
    31             } catch (IOException e) {
    32                 System.out.println("关闭流失败");
    33             }
    34         }

    封装后:

     1  public void copyFile(String srcPath,String destPath){
     2         //建立联系
     3         File src=new File(srcPath);
     4         File dest=new File(destPath);
     5         copyFile(src, dest);
     6     }
     7     public void copyFile(File src,File dest){
     8         //选择流
     9         InputStream is=null;
    10         OutputStream os=null;
    11         try {
    12             is=new FileInputStream(src);
    13             os=new FileOutputStream(dest);
    14             //操作
    15             byte[] b=new byte[1024];
    16             int len=0;
    17             while(-1!=(len=is.read(b))){
    18                 os.write(b, 0, len);
    19             }
    20             os.flush();//强制刷新
    21         } catch (FileNotFoundException e) {
    22             System.out.println("文件找不到");
    23         } catch (IOException e) {
    24             System.out.println("读取失败");
    25             e.printStackTrace();
    26         }finally{
    27             try {
    28                 if(null!=is){
    29                     is.close();
    30                 }
    31                 if(null!=os){
    32                     os.close();
    33                 }
    34             } catch (IOException e) {
    35                 System.out.println("关闭流失败");
    36             }
    37         }
    38     }
  • 相关阅读:
    vue技术分享之你可能不知道的7个秘密
    JVM知识总结-运行时区域划分
    如何使用加多宝(jdb)在linux下调试Java程序
    RabbitMQ 高可用之镜像队列
    Gson格式转换Integer变为Double类型问题解决
    IPv6地址表示方式
    MySQL truncate()函数的使用说明
    Java 实现判断 主机是否能 ping 通
    MySQL 性能优化系列之一 单表预处理
    Linux 查看CPU和内存的使用情况
  • 原文地址:https://www.cnblogs.com/JamKong/p/4447051.html
Copyright © 2011-2022 走看看