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     }
  • 相关阅读:
    linux-2.6.32在mini2440开发板上移植(1)之移植Nand驱动并修改分区信息
    编程错误
    汇编语言程序设计读书笔记(4)- 程序设计基础之一
    汇编语言程序设计读书笔记(3)- 程序范例
    汇编语言程序设计读书笔记(2)- 相关工具64位系统篇
    将博客搬至CSDN
    汇编语言程序设计读书笔记(1)- 相关工具
    CentOS v6.4 64位系统编译linux3.0.8内核错误的解决
    用J-LINK烧写Bootloader到ARM开发板的Nand Flash
    Keil MDK使用J-LINK分别在Sram,Nor Flash以及Sdram中调试代码的原理和方法
  • 原文地址:https://www.cnblogs.com/JamKong/p/4447051.html
Copyright © 2011-2022 走看看