• 文件拷贝的实现


    文件拷贝相当于:
    读取源文件-->写入目的地
     
     
    结合了读取文件跟写入文件
    所以,拷贝文件的步骤如下:
    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     }
  • 相关阅读:
    【转】CTF-Born2root's-WriteUP
    Win10 64位+VS2015+Opencv3.3.0安装配置
    C++ bitset 用法
    未来的一个要参加蓝桥杯,在这里记录下笔记
    一些漏洞测试利用脚本
    Linux下抓取登陆用户密码神器mimipenguin
    免费在线验证码接收平台
    kali linux 安装 Mysql Can't read from messagefile 报错解决方案
    FPGA实现UHS的一些资料
    CYPRESS USB芯片win10驱动
  • 原文地址:https://www.cnblogs.com/JamKong/p/4447051.html
Copyright © 2020-2023  润新知