在Java移動文件夾及其所有子文件與子文件夾可以有如下的一段簡單的方法來說明:
1
2
3
4
5
6
|
public static void moveFolder(String oldPath, String newPath) { //先復制文件 copyFolder(oldPath, newPath); //則刪除源文件,以免復制的時候錯亂 deleteDir( new File(oldPath)); } |
不應該直接剪切文件,防止在剪切的時候出錯,導致這樣那樣的問題。
在Java復制文件夾及其所有子文件與子文件夾,在《【Java】利用文件輸入輸出流完成把一個文件夾內的所有文件拷貝的另一的文件夾的操作》一文中已經詳細說過了。
關鍵是刪除文件夾及其子文件與子文件夾。
在Java中,File類的delete()方法只能刪除為空的文件夾或者單個文件,因此必須遍歷整個文件夾,先從最內層的文件夾中的文件開始,進行遞歸刪除,具體方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// 刪除某個目錄及目錄下的所有子目錄和文件 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 路徑名字符串用于表示目錄或文件。 // 連起來剛好是文件路徑 boolean isDelete = deleteDir( new File(dir, children[i])); // 如果刪完了,沒東西刪,isDelete==false的時候,則跳出此時遞歸 if (!isDelete) { return false ; } } } // 讀到的是一個文件或者是一個空目錄,則可以直接刪除 return dir.delete(); } |
因此,整個方法連起來就是這個樣子,把C盤下的A文件夾及其所有子文件與子文件夾,移動到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 { // 刪除某個目錄及目錄下的所有子目錄和文件 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 路徑名字符串用于表示目錄或文件。 // 連起來剛好是文件路徑 boolean isDelete = deleteDir( new File(dir, children[i])); // 如果刪完了,沒東西刪,isDelete==false的時候,則跳出此時遞歸 if (!isDelete) { return false ; } } } // 讀到的是一個文件或者是一個空目錄,則可以直接刪除 return dir.delete(); } // 復制某個目錄及目錄下的所有子目錄和文件到新文件夾 public static 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 static void moveFolder(String oldPath, String newPath) { // 先復制文件 copyFolder(oldPath, newPath); // 則刪除源文件,以免復制的時候錯亂 deleteDir( new File(oldPath)); } public static void main(String[] args) { moveFolder( "c:/A" , "f:/B" ); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/yongh701/article/details/45070353