在Java移動(dòng)文件夾及其所有子文件與子文件夾可以有如下的一段簡(jiǎn)單的方法來(lái)說(shuō)明:
1
2
3
4
5
6
|
public static void moveFolder(String oldPath, String newPath) { //先復(fù)制文件 copyFolder(oldPath, newPath); //則刪除源文件,以免復(fù)制的時(shí)候錯(cuò)亂 deleteDir( new File(oldPath)); } |
不應(yīng)該直接剪切文件,防止在剪切的時(shí)候出錯(cuò),導(dǎo)致這樣那樣的問(wèn)題。
在Java復(fù)制文件夾及其所有子文件與子文件夾,在《【Java】利用文件輸入輸出流完成把一個(gè)文件夾內(nèi)的所有文件拷貝的另一的文件夾的操作》一文中已經(jīng)詳細(xì)說(shuō)過(guò)了。
關(guān)鍵是刪除文件夾及其子文件與子文件夾。
在Java中,F(xiàn)ile類的delete()方法只能刪除為空的文件夾或者單個(gè)文件,因此必須遍歷整個(gè)文件夾,先從最內(nèi)層的文件夾中的文件開(kāi)始,進(jìn)行遞歸刪除,具體方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// 刪除某個(gè)目錄及目錄下的所有子目錄和文件 public static boolean deleteDir(File dir) { // 如果是文件夾 if (dir.isDirectory()) { // 則讀出該文件夾下的的所有文件 String[] children = dir.list(); // 遞歸刪除目錄中的子目錄下 for ( int i = 0 ; i < children.length; i++) { // File f=new File(String parent ,String child) // parent抽象路徑名用于表示目錄,child 路徑名字符串用于表示目錄或文件。 // 連起來(lái)剛好是文件路徑 boolean isDelete = deleteDir( new File(dir, children[i])); // 如果刪完了,沒(méi)東西刪,isDelete==false的時(shí)候,則跳出此時(shí)遞歸 if (!isDelete) { return false ; } } } // 讀到的是一個(gè)文件或者是一個(gè)空目錄,則可以直接刪除 return dir.delete(); } |
因此,整個(gè)方法連起來(lái)就是這個(gè)樣子,把C盤下的A文件夾及其所有子文件與子文件夾,移動(dòng)到F盤,并且重新命名:
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
|
import java.io.*; public class CutTest { // 刪除某個(gè)目錄及目錄下的所有子目錄和文件 public static boolean deleteDir(File dir) { // 如果是文件夾 if (dir.isDirectory()) { // 則讀出該文件夾下的的所有文件 String[] children = dir.list(); // 遞歸刪除目錄中的子目錄下 for ( int i = 0 ; i < children.length; i++) { // File f=new File(String parent ,String child) // parent抽象路徑名用于表示目錄,child 路徑名字符串用于表示目錄或文件。 // 連起來(lái)剛好是文件路徑 boolean isDelete = deleteDir( new File(dir, children[i])); // 如果刪完了,沒(méi)東西刪,isDelete==false的時(shí)候,則跳出此時(shí)遞歸 if (!isDelete) { return false ; } } } // 讀到的是一個(gè)文件或者是一個(gè)空目錄,則可以直接刪除 return dir.delete(); } // 復(fù)制某個(gè)目錄及目錄下的所有子目錄和文件到新文件夾 public static void copyFolder(String oldPath, String newPath) { try { // 如果文件夾不存在,則建立新文件夾 ( new File(newPath)).mkdirs(); // 讀取整個(gè)文件夾的內(nèi)容到file字符串?dāng)?shù)組,下面設(shè)置一個(gè)游標(biāo)i,不停地向下移開(kāi)始讀這個(gè)數(shù)組 File filelist = new File(oldPath); String[] file = filelist.list(); // 要注意,這個(gè)temp僅僅是一個(gè)臨時(shí)文件指針 // 整個(gè)程序并沒(méi)有創(chuàng)建臨時(shí)文件 File temp = null ; for ( int i = 0 ; i < file.length; i++) { // 如果oldPath以路徑分隔符/或者\(yùn)結(jié)尾,那么則oldPath/文件名就可以了 // 否則要自己oldPath后面補(bǔ)個(gè)路徑分隔符再加文件名 // 誰(shuí)知道你傳遞過(guò)來(lái)的參數(shù)是f:/a還是f:/a/啊? if (oldPath.endsWith(File.separator)) { temp = new File(oldPath + file[i]); } else { temp = new File(oldPath + File.separator + file[i]); } // 如果游標(biāo)遇到文件 if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); // 復(fù)制并且改名 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(); } // 如果游標(biāo)遇到文件夾 if (temp.isDirectory()) { copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { System.out.println( "復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)" ); } } public static void moveFolder(String oldPath, String newPath) { // 先復(fù)制文件 copyFolder(oldPath, newPath); // 則刪除源文件,以免復(fù)制的時(shí)候錯(cuò)亂 deleteDir( new File(oldPath)); } public static void main(String[] args) { moveFolder( "c:/A" , "f:/B" ); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/yongh701/article/details/45070353