-
Notifications
You must be signed in to change notification settings - Fork 6
/
file and directory copy
393 lines (349 loc) · 10.5 KB
/
file and directory copy
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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package com.jp.bz;
import java.io.*;
public class FileCopy {
FileInputStream FIS;
FileOutputStream FOS;
final int BUFFER_SIZE = 1024 * 5;
// 复制文件
public void copyFile(String sourceFile, String targetFile)
throws IOException {
// 新建文件输入流并对它进行缓冲
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff = new BufferedInputStream(input);
// 新建文件输出流并对它进行缓冲
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff = new BufferedOutputStream(output);
// 缓冲数组
byte[] b = new byte[BUFFER_SIZE];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
// 关闭流
inBuff.close();
outBuff.close();
output.close();
input.close();
}
/**
* 获取文件后缀 如:.java .jsp
*
* @param filename
* @return
*/
public String getSuffix(String filename) {
if (filename != null) {
int index = filename.lastIndexOf(".");
if (index >= 0) {
return filename.substring(filename.lastIndexOf("."));
}
}
return null;
}
/**
* 获取文件名,不包括后缀 test
*
* @param filename
* @return
*/
public String getFilename(String filename) {
if (filename != null) {
int lastPointIndex = filename.lastIndexOf(".");
int lastSeparatorIndex = filename.lastIndexOf(File.separator);
if (lastPointIndex >= 0) {
return filename.substring(lastSeparatorIndex + 1,
lastPointIndex);
} else {
return filename.substring(lastSeparatorIndex + 1);
}
}
return null;
}
/**
* 获取文件名,包括后缀 test.java
*
* @param filename
* @return
*/
public String getFilenameIncludeSuffix(String filename) {
if (filename != null) {
return filename.substring(filename.lastIndexOf(File.separator) + 1);
}
return null;
}
}
package com.jp.bz;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class DirectoryCopy {
FileInputStream FIS;
FileOutputStream FOS;
String beforeSuffix = ".java";
String afterSuffix = ".j";
public DirectoryCopy() {
}
// 文件夾拷貝核心函數
public boolean copyDirectory(String SrcDirectoryPath,
String DesDirectoryPath) {
return copyDirectory(SrcDirectoryPath, DesDirectoryPath, beforeSuffix, afterSuffix);
}
// 文件夾拷貝核心函數
public boolean copyDirectory(String SrcDirectoryPath,
String DesDirectoryPath, String beforeSuffix, String afterSuffix) {
try {
// 創建不存在的目錄
File F0 = new File(DesDirectoryPath);
if (!F0.exists()) {
if (!F0.mkdir()) {
System.out.println("目標文件夾不存,創建失敗!");
}
}
File F = new File(SrcDirectoryPath);
File[] allFile = F.listFiles(); // 取得當前目錄下面的所有文件,將其放在文件數組中
int totalNum = allFile.length; // 取得當前文件夾中有多少文件(包括文件夾)
String srcName = "";
String desName = "";
int currentFile = 0;
// 一個一個的拷貝文件
for (currentFile = 0; currentFile < totalNum; currentFile++) {
if (!allFile[currentFile].isDirectory()) {
// 如果是文件是采用處理文件的方式
FileCopy FC = new FileCopy();
String filename = allFile[currentFile].toString();
String suffix = FC.getSuffix(filename);
if (suffix != null && beforeSuffix.equals(suffix)) {
desName = DesDirectoryPath + "\\"
+ FC.getFilename(filename) + afterSuffix;
} else {
desName = DesDirectoryPath + "\\"
+ FC.getFilenameIncludeSuffix(filename);
}
srcName = filename;
FC.copyFile(srcName, desName);
}
// 如果是文件夾就采用遞歸處理
else {
// 利用遞歸讀取文件夾中的子文件下的內容,再讀子文件夾下面的子文件夾下面的內容...
if (copyDirectory(
allFile[currentFile].getPath().toString(),
DesDirectoryPath + "\\"
+ allFile[currentFile].getName().toString(), beforeSuffix, afterSuffix)) {
} else {
System.out.println("SubDirectory Copy Error!");
}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
DirectoryCopy directoryCopy = new DirectoryCopy();
String src = "D:\\test\\BD";
String dest = "D:\\test\\xjc";
String beforeSuffix = ".java";
String afterSuffix = ".j";
File F = new File(src);
if (F.exists()) {
if (!F.isDirectory()) {
System.out.println("請輸入一個目錄的全路徑!");
System.exit(0);
}
} else {
}
if (directoryCopy.copyDirectory(src, dest, beforeSuffix, afterSuffix)) {
System.out.println("Directory Copy Successfully!");
} else {
System.out.println("Directory Copy error!");
}
}
}
package com.jp.bz;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class DirectoryCopy {
FileInputStream FIS;
FileOutputStream FOS;
String beforeSuffix = ".java";
String afterSuffix = ".j";
HashMap<String, String> suffix = new HashMap<String, String>();
public DirectoryCopy() {
}
public void initSuffix() {
suffix.put(".java", ".j");
suffix.put(".doc", ".d");
suffix.put(".docx", ".dx");
suffix.put(".html", ".hl");
suffix.put(".htm", ".hm");
suffix.put(".txt", ".tt");
suffix.put(".TXT", ".tt");
suffix.put(".xls", ".xs");
suffix.put(".xlsx", ".xx");
suffix.put(".pdf", ".pf");
suffix.put(".ppt", ".pt");
suffix.put(".pptx", ".px");
}
// 文件夾拷貝核心函數
public boolean copyDirectory(String SrcDirectoryPath,
String DesDirectoryPath) {
return copyDirectory(SrcDirectoryPath, DesDirectoryPath, beforeSuffix,
afterSuffix);
}
public String getAfterName(String name, boolean flag) {
initSuffix();
Set<String> keys = suffix.keySet();
Iterator<String> iterator = keys.iterator();
String beforeSuffix = null;
String afterSuffix = name;
while (iterator.hasNext()) {
String tmp = iterator.next();
if (flag) {
beforeSuffix = tmp;
} else {
beforeSuffix = suffix.get(tmp);
}
if (beforeSuffix.equals(name)) {
if (flag) {
afterSuffix = suffix.get(beforeSuffix);
} else {
afterSuffix = tmp;
}
break;
}
}
return afterSuffix;
}
// 文件夾拷貝核心函數
public boolean copyDirectory(String SrcDirectoryPath,
String DesDirectoryPath, boolean flag) {
try {
// 創建不存在的目錄
File F0 = new File(DesDirectoryPath);
if (!F0.exists()) {
if (!F0.mkdir()) {
System.out.println("目標文件夾不存,創建失敗!");
}
}
File F = new File(SrcDirectoryPath);
File[] allFile = F.listFiles(); // 取得當前目錄下面的所有文件,將其放在文件數組中
int totalNum = allFile.length; // 取得當前文件夾中有多少文件(包括文件夾)
String srcName = "";
String desName = "";
int currentFile = 0;
// 一個一個的拷貝文件
for (currentFile = 0; currentFile < totalNum; currentFile++) {
if (!allFile[currentFile].isDirectory()) {
// 如果是文件是采用處理文件的方式
FileCopy FC = new FileCopy();
String filename = allFile[currentFile].toString();
String suffix = FC.getSuffix(filename);
String afterSuffix = getAfterName(suffix, flag);
desName = DesDirectoryPath + "\\"
+ FC.getFilename(filename) + afterSuffix;
srcName = filename;
FC.copyFile(srcName, desName);
}
// 如果是文件夾就采用遞歸處理
else {
// 利用遞歸讀取文件夾中的子文件下的內容,再讀子文件夾下面的子文件夾下面的內容...
if (copyDirectory(
allFile[currentFile].getPath().toString(),
DesDirectoryPath + "\\"
+ allFile[currentFile].getName().toString(),
flag)) {
} else {
System.out.println("SubDirectory Copy Error!");
}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
// 文件夾拷貝核心函數
public boolean copyDirectory(String SrcDirectoryPath,
String DesDirectoryPath, String beforeSuffix, String afterSuffix) {
try {
// 創建不存在的目錄
File F0 = new File(DesDirectoryPath);
if (!F0.exists()) {
if (!F0.mkdir()) {
System.out.println("目標文件夾不存,創建失敗!");
}
}
File F = new File(SrcDirectoryPath);
File[] allFile = F.listFiles(); // 取得當前目錄下面的所有文件,將其放在文件數組中
int totalNum = allFile.length; // 取得當前文件夾中有多少文件(包括文件夾)
String srcName = "";
String desName = "";
int currentFile = 0;
// 一個一個的拷貝文件
for (currentFile = 0; currentFile < totalNum; currentFile++) {
if (!allFile[currentFile].isDirectory()) {
// 如果是文件是采用處理文件的方式
FileCopy FC = new FileCopy();
String filename = allFile[currentFile].toString();
String suffix = FC.getSuffix(filename);
if (suffix != null && beforeSuffix.equals(suffix)) {
desName = DesDirectoryPath + "\\"
+ FC.getFilename(filename) + afterSuffix;
} else {
desName = DesDirectoryPath + "\\"
+ FC.getFilenameIncludeSuffix(filename);
}
srcName = filename;
FC.copyFile(srcName, desName);
}
// 如果是文件夾就采用遞歸處理
else {
// 利用遞歸讀取文件夾中的子文件下的內容,再讀子文件夾下面的子文件夾下面的內容...
if (copyDirectory(
allFile[currentFile].getPath().toString(),
DesDirectoryPath + "\\"
+ allFile[currentFile].getName().toString(),
beforeSuffix, afterSuffix)) {
} else {
System.out.println("SubDirectory Copy Error!");
}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
DirectoryCopy directoryCopy = new DirectoryCopy();
String src = "E:\\test";
String dest = "c:\\abd";
String beforeSuffix = ".java";
String afterSuffix = ".j";
directoryCopy.initSuffix();
boolean flag = false;
File F = new File(src);
if (F.exists()) {
if (!F.isDirectory()) {
System.out.println("請輸入一個目錄的全路徑!");
System.exit(0);
}
} else {
}
if (directoryCopy.copyDirectory(src, dest, flag)) {
System.out.println("Directory Copy Successfully!");
} else {
System.out.println("Directory Copy error!");
}
}
}