文件夾創(chuàng)建,文件夾刪除,文件夾拷貝
Posted on 2009-04-11 01:21 笑看風(fēng)云 閱讀(325) 評(píng)論(0) 編輯 收藏 所屬分類: Java
1
package test;
2
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileOutputStream;
7
import java.io.FileReader;
8
9
import junit.framework.TestCase;
10
11
public class Filetest extends TestCase {
12
13
// 創(chuàng)建文件夾
14
public String creatdir(String dir) {
15
File dirFile;
16
boolean bFile;
17
18
bFile = false;
19
//指定創(chuàng)建文件夾的目錄和名稱
20
dirFile = new File(dir);
21
bFile = dirFile.exists();
22
23
if (bFile == true) {
24
System.out.println("文件夾已經(jīng)存在.");
25
} else {
26
System.out.println("文件夾不存在,開始創(chuàng)建");
27
bFile = dirFile.mkdirs();
28
if (bFile == true) {
29
System.out.println("創(chuàng)建文件夾成功!");
30
} else {
31
System.out.println("創(chuàng)建文件夾失敗.");
32
System.exit(1);
33
}
34
}
35
36
return dir;
37
}
38
39
//拷貝文件夾
40
public void copyDir(File from, File to) {
41
// 判斷需要拷貝到的路徑下的文件夾是否存在
42
if (!to.exists()) {
43
// 創(chuàng)建文件夾
44
to.mkdirs();
45
}
46
File[] files = from.listFiles();
47
for (int i = 0; i < files.length; i++) {//遍歷文件夾
48
File file1 = files[i];
49
File file2 = new File(to.getPath() + File.separator
50
+ files[i].getName());
51
if (!file1.isDirectory()) { //拷貝文件
52
copyFile(file1, file2);
53
} else {
54
copyDir(file1, file2);
55
}
56
}
57
58
}
59
60
//拷貝文件
61
public void copyFile(File src, File dest) {
62
try {
63
System.out.println(src.getAbsoluteFile() + " -> "
64
+ dest.getAbsoluteFile());
65
FileInputStream in = new FileInputStream(src);
66
FileOutputStream out = new FileOutputStream(dest);
67
byte[] buffer = new byte[1024];
68
while (in.read(buffer) != -1) {
69
out.write(buffer);
70
}
71
out.close();
72
in.close();
73
System.out.println("文件拷貝成功");
74
} catch (Exception e) {
75
System.out.println("文件拷貝失敗");
76
}
77
78
}
79
80
// 刪除文件夾
81
// param folderPath 文件夾完整絕對(duì)路徑
82
public void delFolder(String folderPath) {
83
try {
84
this.delAllFile(folderPath); // 刪除完里面所有內(nèi)容(第一步)
85
String filePath = folderPath;
86
filePath = filePath.toString();
87
java.io.File myFilePath = new java.io.File(filePath);
88
myFilePath.delete(); // 刪除空文件夾(第二步)
89
} catch (Exception e) {
90
e.printStackTrace();
91
}
92
}
93
94
// 刪除指定文件夾下所有文件(第一步)
95
// param path 文件夾完整絕對(duì)路徑
96
public boolean delAllFile(String path) {
97
boolean flag = false;
98
File file = new File(path);
99
// 文件夾是否存在
100
if (!file.exists()) {
101
return flag;
102
}
103
// 如果不是目錄
104
if (!file.isDirectory()) {
105
return flag;
106
}
107
String[] tempList = file.list();
108
File temp = null;
109
for (int i = 0; i < tempList.length; i++) {//遍歷所有文件夾以及里面的文件
110
if (path.endsWith(File.separator)) { //文件分隔符,各個(gè)操作系統(tǒng)不一樣,如WIndows的是"\",而Linux的是"/"
111
temp = new File(path + tempList[i]);
112
} else {
113
temp = new File(path + File.separator + tempList[i]);
114
}
115
if (temp.isFile()) {//刪除文件
116
temp.delete();
117
}
118
if (temp.isDirectory()) {
119
delAllFile(path + "/" + tempList[i]);// 先刪除文件夾里面的文件
120
delFolder(path + "/" + tempList[i]);// 再刪除空文件夾
121
flag = true;
122
}
123
}
124
return flag;
125
}
126
127
//讀文件內(nèi)容
128
public File readfile(String path) {
129
BufferedReader bufread;
130
String read = "";
131
String readStr = "";
132
File file = new File(path);
133
try {
134
FileReader fileread = new FileReader(file);
135
bufread = new BufferedReader(fileread);
136
while ((read = bufread.readLine()) != null) {
137
readStr = readStr + read;
138
}
139
bufread.close();
140
} catch (Exception d) {
141
System.out.println(d.getMessage());
142
}
143
System.out.println("###readStr:" + readStr);
144
return file;
145
}
146
147
public void testcopyfile() {
148
149
Filetest t = new Filetest();
150
String a = "D://test1";
151
String b = "D://test2";
152
// t.copyDir(t.readfile(a), t.readfile(b));
153
// t.copyDir(new File(a), new File(b));
154
t.delFolder(a);
155
156
}
157
}
158

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

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158
