一、基本目標
使用java完成如下的操作:
把一個文件夾內的所有文件拷貝的另一的文件夾,例如,在f盤中有a與b兩個文件夾:
f:/a里面有一堆文件,運行java程序之后就會全部復制到f:/b,并且完成重命名,在所有文件之前加rename_的前綴,如果里面有文件夾,則文件夾不重命名,里面的文件進行重命名,同樣在所有文件之前加rename_的前綴:
二、制作過程
1、首先主函數非常簡單,就是調用了上面filetest類中的copyfolder函數
1
2
3
4
5
6
|
public class filecopy { public static void main(string args[]) { new filetest().copyfolder( "f:/a" , "f:/b" ); } } |
值得注意的是,這個的傳遞過去的參數的路徑寫法,在java中,f:/a是沒有問題的,f:\a也是沒有問題的,但是由于\在字符串表達的時候,必須轉移,所以你必須寫成f:\\a
2、整個程序的關鍵在這個filetest類中的copyfolder函數,這個類里面就這個函數-_-!而且注意在程序開頭引入java.io.*;由于用到了輸入輸出流
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
|
class filetest { public void copyfolder(string oldpath, string newpath) { try { // 如果文件夾不存在,則建立新文件夾 ( new file(newpath)).mkdirs(); //讀取整個文件夾的內容到file字符串數組,下面設置一個游標i,不停地向下移開始讀這個數組 file filelist = new file(oldpath); string[] file = filelist.list(); //要注意,這個temp僅僅是一個臨時文件指針 //整個程序并沒有創建臨時文件 file temp = null ; for ( int i = 0 ; i < file.length; i++) { //如果oldpath以路徑分隔符/或者\結尾,那么則oldpath/文件名就可以了 //否則要自己oldpath后面補個路徑分隔符再加文件名 //誰知道你傳遞過來的參數是f:/a還是f:/a/啊? 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 + "/" + "rename_" + (temp.getname()).tostring()); byte [] bufferarray = new byte [ 1024 * 64 ]; int prereadlength; while ((prereadlength = input.read(bufferarray)) != - 1 ) { output.write(bufferarray, 0 , prereadlength); } output.flush(); output.close(); input.close(); } //如果游標遇到文件夾 if (temp.isdirectory()) { copyfolder(oldpath + "/" + file[i], newpath + "/" + file[i]); } } } catch (exception e) { system.out.println( "復制整個文件夾內容操作出錯" ); } } } |
可能游標遇到文件部分有點難以理解,其實是這樣的,首先設置一個文件的輸入流,指定從游標遇到的文件中輸入,再指定輸出到newpath/rename_舊文件的文件名這個文件目錄,之后,設置一個緩沖數組,文件輸入流對于自己要讀取的文件,每次調用read方法,它都會向后繼續上一次讀取的位置繼續讀取緩沖數組bufferarray的長度的內容,把讀取到的內容存儲到緩沖數組,覆蓋緩沖數組之前的所有內容,然后文件輸出流會把緩沖數組的所有內容輸出的指定的位置,直到文件輸入流遇到了-1。
至于文件輸入流為何能這樣按順序,每次都會向后繼續上一次讀取的位置繼續讀取,那是因為當要進行文件的讀取,java封裝的fileinputstream.read方法也會調用操作系統的api依次讀取這些數據。在讀取文件數據的時候必須是順序的,不可能說先讀取第一個字節,后讀取倒數第二個字節。循環讀取的時候就read方法將讀取的位置++,因此造成每次read都是順序讀取后面的字節,直到遇到文件末尾標記。
當游標遇到文件夾則重新調用自己完成同樣的操作即可,這就是所謂的迭代。
3、因此整個程序如下:
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
|
import java.io.*; /** * * @param oldpath 被拷貝的目錄 * @param newpath 要拷貝到的目錄 * */ class filetest { public void copyfolder(string oldpath, string newpath) { try { // 如果文件夾不存在,則建立新文件夾 ( new file(newpath)).mkdirs(); //讀取整個文件夾的內容到file字符串數組,下面設置一個游標i,不停地向下移開始讀這個數組 file filelist = new file(oldpath); string[] file = filelist.list(); //要注意,這個temp僅僅是一個臨時文件指針 //整個程序并沒有創建臨時文件 file temp = null ; for ( int i = 0 ; i < file.length; i++) { //如果oldpath以路徑分隔符/或者\結尾,那么則oldpath/文件名就可以了 //否則要自己oldpath后面補個路徑分隔符再加文件名 //誰知道你傳遞過來的參數是f:/a還是f:/a/啊? 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 + "/" + "rename_" + (temp.getname()).tostring()); byte [] bufferarray = new byte [ 1024 * 64 ]; int prereadlength; while ((prereadlength = input.read(bufferarray)) != - 1 ) { output.write(bufferarray, 0 , prereadlength); } output.flush(); output.close(); input.close(); } //如果游標遇到文件夾 if (temp.isdirectory()) { copyfolder(oldpath + "/" + file[i], newpath + "/" + file[i]); } } } catch (exception e) { system.out.println( "復制整個文件夾內容操作出錯" ); } } } public class filecopy { public static void main(string args[]) { new filetest().copyfolder( "f:/a" , "f:/b" ); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/yongh701/article/details/42964557