当前位置: 移动技术网 > IT编程>开发语言>Java > Java IO编程——文件拷贝

Java IO编程——文件拷贝

2019年11月10日  | 移动技术网IT编程  | 我要评论

在操作系统里面有一个copy命令,这个命令的主要功能是可以实现文件的拷贝处理,现在要求模拟这个命令,通过初始化参数输入拷贝的源文件路径与拷贝的目标路径实现文件的拷贝处理。

需求分析:

   ·需要实现文件的拷贝操作,那么这种拷贝就有可能拷贝各种类型的文件,所以肯定使用字节流;

   ·在进行拷贝的时候有可能需要考虑到大文件的拷贝问题;

实现方案:

   ·方案一:使用inputstream将全部要拷贝的内容直接读取到程序里面,而后一次性的输出到目标文件;

  |- 如果现在拷贝的文件很大,基本上程序就死了;

   ·方案二:采用部分拷贝,读取一部分输出一部分数据,如果现在要采用第二种做法,核心的操作方法:

      |- inputstream:public int read​(byte[] b) throws ioexception;

      |- outputstream:public void write​(byte[] b,int off, int len) throws ioexception;

范例:实现文件拷贝处理

 1 import java.io.file;
 2 import java.io.fileinputstream;
 3 import java.io.fileoutputstream;
 4 import java.io.inputstream;
 5 import java.io.outputstream;
 6 class fileutil {    // 定义一个文件操作的工具类
 7     private file srcfile ; // 源文件路径
 8     private file desfile ; // 目标文件路径
 9     public fileutil(string src,string des) {
10         this(new file(src),new file(des)) ;
11     }
12     public fileutil(file srcfile,file desfile) {
13         this.srcfile = srcfile ;
14         this.desfile = desfile ;
15     }
16     public boolean copy() throws exception {    // 文件拷贝处理
17         if (!this.srcfile.exists()) {    // 源文件必须存在!
18             system.out.println("拷贝的源文件不存在!");
19             return false ; // 拷贝失败
20         }
21         if (!this.desfile.getparentfile().exists()) { 
22             this.desfile.getparentfile().mkdirs() ; // 创建父目录
23         }
24         byte data [] = new byte[1024] ; // 开辟一个拷贝的缓冲区
25         inputstream input = null ;
26         outputstream output = null ;
27         try {
28             input = new fileinputstream(this.srcfile) ;
29             output = new fileoutputstream(this.desfile) ;
30             int len = 0 ;
31             // 1、读取数据到数组之中,随后返回读取的个数、len = input.read(data
32             // 2、判断个数是否是-1,如果不是则进行写入、(len = input.read(data)) != -1
33             while ((len = input.read(data)) != -1) {
34                 output.write(data, 0, len);
35             }
36             return true ; 
37         } catch (exception e) {
38             throw e ;
39         } finally {
40             if (input != null) {
41                 input.close();    
42             } 
43             if (output != null) {
44                 output.close() ;
45             }
46         }
47     }
48 }
49 public class javaapidemo {
50     public static void main(string[] args) throws exception {
51         if (args.length != 2) {    // 程序执行出错
52             system.out.println("命令执行错误,执行结构:java javaapidemo 拷贝源文件路径 拷贝目标文件路径");
53             system.exit(1); 
54         }
55         long start = system.currenttimemillis() ;
56         fileutil fu = new fileutil(args[0],args[1]) ;
57         system.out.println(fu.copy() ? "文件拷贝成功!" : "文件拷贝失败!");
58         long end = system.currenttimemillis() ;
59         system.out.println("拷贝完成的时间:" + (end - start));
60     }
61 }

  

  

    但是需要注意的是,以上的做法是属于文件拷贝的最原始的实现,而从jdk1.9开始inputstream和reader类中都追加有数据转存的处理操作方法:

      ·inputstream:public long transferto​(outputstream out) throws ioexception;

      ·reader:public long transferto​(writer out) throws ioexception;

范例:使用转存的方式处理

 1 import java.io.file;
 2 import java.io.fileinputstream;
 3 import java.io.fileoutputstream;
 4 import java.io.inputstream;
 5 import java.io.outputstream;
 6 class fileutil {    // 定义一个文件操作的工具类
 7     private file srcfile ; // 源文件路径
 8     private file desfile ; // 目标文件路径
 9     public fileutil(string src,string des) {
10         this(new file(src),new file(des)) ;
11     }
12     public fileutil(file srcfile,file desfile) {
13         this.srcfile = srcfile ;
14         this.desfile = desfile ;
15     }
16     public boolean copy() throws exception {    // 文件拷贝处理
17         if (!this.srcfile.exists()) {    // 源文件必须存在!
18             system.out.println("拷贝的源文件不存在!");
19             return false ; // 拷贝失败
20         }
21         if (!this.desfile.getparentfile().exists()) { 
22             this.desfile.getparentfile().mkdirs() ; // 创建父目录
23         }
24         inputstream input = null ;
25         outputstream output = null ;
26         try {
27             input = new fileinputstream(this.srcfile) ;
28             output = new fileoutputstream(this.desfile) ;
29             input.transferto(output) ;
30             return true ; 
31         } catch (exception e) {
32             throw e ;
33         } finally {
34             if (input != null) {
35                 input.close();    
36             } 
37             if (output != null) {
38                 output.close() ;
39             }
40         }
41     }
42 }
43 public class javaapidemo {
44     public static void main(string[] args) throws exception {
45         if (args.length != 2) {    // 程序执行出错
46             system.out.println("命令执行错误,执行结构:java javaapidemo 拷贝源文件路径 拷贝目标文件路径");
47             system.exit(1); 
48         }
49         long start = system.currenttimemillis() ;
50         fileutil fu = new fileutil(args[0],args[1]) ;
51         system.out.println(fu.copy() ? "文件拷贝成功!" : "文件拷贝失败!");
52         long end = system.currenttimemillis() ;
53         system.out.println("拷贝完成的时间:" + (end - start));
54     }
55 }

  此时千万要注意程序的运行版本问题。如果说现在对此程序要求进一步扩展,可以实现一个文件目录的拷贝呢?一旦进行了文件目录的拷贝还需要拷贝所有的子目录中的文件。


 

范例:文件夹拷贝

 1 import java.io.file;
 2 import java.io.fileinputstream;
 3 import java.io.fileoutputstream;
 4 import java.io.inputstream;
 5 import java.io.outputstream;
 6 class fileutil {    // 定义一个文件操作的工具类
 7     private file srcfile ; // 源文件路径
 8     private file desfile ; // 目标文件路径
 9     public fileutil(string src,string des) {
10         this(new file(src),new file(des)) ;
11     }
12     public fileutil(file srcfile,file desfile) {
13         this.srcfile = srcfile ;
14         this.desfile = desfile ;
15     }
16     public boolean copydir() throws exception {
17         try {
18             this.copyimpl(this.srcfile) ;
19             return true ;
20         } catch (exception e) {
21             return false ; 
22         }
23     }
24     private void copyimpl(file file) throws exception {    // 递归操作
25         if (file.isdirectory()) {    // 是目录
26             file results [] = file.listfiles() ; // 列出全部目录组成
27             if (results != null) {
28                 for (int x = 0 ; x < results.length ; x ++) {
29                     copyimpl(results[x]) ;
30                 }
31             }
32         } else {    // 是文件
33             string newfilepath = file.getpath().replace(this.srcfile.getpath() + file.separator, "") ;
34             file newfile = new file(this.desfile,newfilepath) ; // 拷贝的目标路径
35             this.copyfileimpl(file, newfile) ;
36         }
37     }
38     private boolean copyfileimpl(file srcfile,file desfile) throws exception {
39         if (!desfile.getparentfile().exists()) { 
40             desfile.getparentfile().mkdirs() ; // 创建父目录
41         }
42         inputstream input = null ;
43         outputstream output = null ;
44         try {
45             input = new fileinputstream(srcfile) ;
46             output = new fileoutputstream(desfile) ;
47             input.transferto(output) ;
48             return true ;
49         } catch (exception e) {
50             throw e ;
51         } finally {
52             if (input != null) {
53                 input.close();    
54             } 
55             if (output != null) {
56                 output.close() ;
57             }
58         }
59     }
60     
61     public boolean copy() throws exception {    // 文件拷贝处理
62         if (!this.srcfile.exists()) {    // 源文件必须存在!
63             system.out.println("拷贝的源文件不存在!");
64             return false ; // 拷贝失败
65         }
66         return this.copyfileimpl(this.srcfile, this.desfile) ;
67     }
68 }
69 public class javaapidemo {
70     public static void main(string[] args) throws exception {
71         if (args.length != 2) {    // 程序执行出错
72             system.out.println("命令执行错误,执行结构:java javaapidemo 拷贝源文件路径 拷贝目标文件路径");
73             system.exit(1); 
74         }
75         long start = system.currenttimemillis() ;
76         fileutil fu = new fileutil(args[0],args[1]) ;
77         if (new file(args[0]).isfile()) {    // 文件拷贝
78             system.out.println(fu.copy() ? "文件拷贝成功!" : "文件拷贝失败!");
79         } else {    // 目录拷贝
80             system.out.println(fu.copydir() ? "文件拷贝成功!" : "文件拷贝失败!");
81         }
82         long end = system.currenttimemillis() ;
83         system.out.println("拷贝完成的时间:" + (end - start));
84     }
85 }
本程序是io操作的核心代码,本程序可以理解整个的io处理机制就非常容易理解了。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网