1
/**
2
* 拷貝一個(gè)目錄或者文件到指定路徑下
3
*
4
* @param source
5
* @param target
6
*/
7
public static void copy(File source, File target)
8
{
9
File tarpath = new File(target, source.getName());
10
if (source.isDirectory())
11
{
12
tarpath.mkdir();
13
File[] dir = source.listFiles();
14
for (int i = 0; i < dir.length; i++)
15
{
16
copy(dir[i], tarpath);
17
}
18
}
19
else
20
{
21
try
22
{
23
InputStream is = new FileInputStream(source);
24
OutputStream os = new FileOutputStream(tarpath);
25
byte[] buf = new byte[1024];
26
int len = 0;
27
while ((len = is.read(buf)) != -1)
28
{
29
os.write(buf, 0, len);
30
}
31
is.close();
32
os.close();
33
}
34
catch (FileNotFoundException e)
35
{
36
e.printStackTrace();
37
}
38
catch (IOException e)
39
{
40
e.printStackTrace();
41
}
42
}
43
}

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
