盡管java提供了一個可以處理文件的io操作類。 但是沒有一個復制文件的方法。 復制文件是一個重要的操作,當你的程序必須處理很多文件相關的時候。 然而有幾種方法可以進行java文件復制操作,下面列舉出4中最受歡迎的方式。
1. 使用filestreams復制
這是最經典的方式將一個文件的內容復制到另一個文件中。 使用fileinputstream讀取文件a的字節,使用fileoutputstream寫入到文件b。 這是第一個方法的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private static void copyfileusingfilestreams(file source, file dest) throws ioexception { inputstream input = null ; outputstream output = null ; try { input = new fileinputstream(source); output = new fileoutputstream(dest); byte [] buf = new byte [ 1024 ]; int bytesread; while ((bytesread = input.read(buf)) > 0 ) { output.write(buf, 0 , bytesread); } } finally { input.close(); output.close(); } } |
正如你所看到的我們執行幾個讀和寫操作try的數據,所以這應該是一個低效率的,下一個方法我們將看到新的方式。
2. 使用filechannel復制
java nio包括transferfrom方法,根據文檔應該比文件流復制的速度更快。 這是第二種方法的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
|
private static void copyfileusingfilechannels(file source, file dest) throws ioexception { filechannel inputchannel = null ; filechannel outputchannel = null ; try { inputchannel = new fileinputstream(source).getchannel(); outputchannel = new fileoutputstream(dest).getchannel(); outputchannel.transferfrom(inputchannel, 0 , inputchannel.size()); } finally { inputchannel.close(); outputchannel.close(); } } |
3. 使用commons io復制
apache commons io提供拷貝文件方法在其fileutils類,可用于復制一個文件到另一個地方。它非常方便使用apache commons fileutils類時,您已經使用您的項目。基本上,這個類使用java nio filechannel內部。 這是第三種方法的代碼:
1
2
3
4
|
private static void copyfileusingapachecommonsio(file source, file dest) throws ioexception { fileutils.copyfile(source, dest); } |
4. 使用java7的files類復制
如果你有一些經驗在java 7中你可能會知道,可以使用復制方法的files類文件,從一個文件復制到另一個文件。 這是第四個方法的代碼:
1
2
3
4
|
private static void copyfileusingjava7files(file source, file dest) throws ioexception { files.copy(source.topath(), dest.topath()); } |
下面看下java拷貝文件到另一個目錄下的實現代碼,具體代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package com.util; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.inputstream; public class testhtml { /** * 復制單個文件 * @param oldpath string 原文件路徑 如:c:/fqf.txt * @param newpath string 復制后路徑 如:f:/fqf.txt * @return boolean */ public void copyfile(string oldpath, string newpath) { try { int bytesum = 0 ; int byteread = 0 ; file oldfile = new file(oldpath); if (oldfile.exists()) { //文件存在時 inputstream instream = new fileinputstream(oldpath); //讀入原文件 fileoutputstream fs = new fileoutputstream(newpath); byte [] buffer = new byte [ 1444 ]; int length; while ( (byteread = instream.read(buffer)) != - 1 ) { bytesum += byteread; //字節數 文件大小 system.out.println(bytesum); fs.write(buffer, 0 , byteread); } instream.close(); } } catch (exception e) { system.out.println( "復制單個文件操作出錯" ); e.printstacktrace(); } } /** * 復制整個文件夾內容 * @param oldpath string 原文件路徑 如:c:/fqf * @param newpath string 復制后路徑 如:f:/fqf/ff * @return boolean */ public void copyfolder(string oldpath, string newpath) { try { ( new file(newpath)).mkdirs(); //如果文件夾不存在 則建立新文件夾 file a= new file(oldpath); string[] file=a.list(); file temp= null ; for ( int i = 0 ; i < file.length; i++) { if (oldpath.endswith(file.separator)){ temp= new file(oldpath+file[i]); } else { temp= new file(oldpath+file.separator+file[i]); } if (temp.isfile()){ fileinputstream input = new fileinputstream(temp); fileoutputstream output = new fileoutputstream(newpath + "/" + (temp.getname()).tostring()); byte [] b = new byte [ 1024 * 5 ]; int len; while ( (len = input.read(b)) != - 1 ) { output.write(b, 0 , len); } output.flush(); output.close(); input.close(); } if (temp.isdirectory()){ //如果是子文件夾 copyfolder(oldpath+ "/" +file[i],newpath+ "/" +file[i]); } } } catch (exception e) { system.out.println( "復制整個文件夾內容操作出錯" ); e.printstacktrace(); } } public static void main(string[] args) throws exception { // //這是你的源文件,本身是存在的 // file beforefile = new file("c:/users/administrator/desktop/untitled-2.html"); // // //這是你要保存之后的文件,是自定義的,本身不存在 // file afterfile = new file("c:/users/administrator/desktop/jiekou0/untitled-2.html"); // // //定義文件輸入流,用來讀取beforefile文件 // fileinputstream fis = new fileinputstream(beforefile); // // //定義文件輸出流,用來把信息寫入afterfile文件中 // fileoutputstream fos = new fileoutputstream(afterfile); // // //文件緩存區 // byte[] b = new byte[1024]; // //將文件流信息讀取文件緩存區,如果讀取結果不為-1就代表文件沒有讀取完畢,反之已經讀取完畢 // while(fis.read(b)!=-1){ // //將緩存區中的內容寫到afterfile文件中 // fos.write(b); // fos.flush(); // } string oldpath= "c:/users/administrator/desktop/untitled-2.html" ; string newpath= "c:/users/administrator/desktop/jiekou0/untitled-2.html" ; testhtml t= new testhtml(); t.copyfile(oldpath, newpath); } } |
總結
以上所述是小編給大家介紹的java復制文件的4種方式及拷貝文件到另一個目錄下的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!