java建立新文件夾和實現(xiàn)文件的拷貝
1
import java.io.*;
2
3
public class Demo {
4
public static void main(String[] args) {
5
File dirFile;
6
File tempFile;
7
boolean bFile;
8
String sFileName;
9
10
bFile = false;
11
12
try {
13
dirFile = new File("E:\\test");
14
bFile = dirFile.exists();
15
16
if (bFile == true) {
17
System.out.println("The folder exists.");
18
} else {
19
System.out
20
.println("The folder do not exist,now trying to create a one
");
21
bFile = dirFile.mkdir();
22
if (bFile == true) {
23
System.out.println("Create successfully!");
24
} else {
25
System.out
26
.println("Disable to make the folder,please check the disk is full or not.");
27
System.exit(1);
28
}
29
}
30
31
System.out.println("Now we put files in to the folder
");
32
33
for (int i = 0; i < 5; i++) {
34
sFileName = new String("E:\\test\\");
35
sFileName += String.valueOf(i);
36
tempFile = new File(sFileName);
37
bFile = tempFile.createNewFile();
38
}
39
} catch (IOException e) {
40
// Exception hadler
41
}
42
43
if (bFile == true)
44
System.out.println("Successfully put files in to the folder!");
45
else
46
System.out.println("Sorry sir,i don't finish the job!");
47
}
48
}
49

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

1 import java.io.*;
2 import java.text.*;
3 import java.util.*;
4
5 /**
6 * @author ljt
7 *
8 * To change this generated comment edit the template variable "typecomment":
9 * Window>Preferences>Java>Templates.
10 * To enable and disable the creation of type comments go to
11 * Window>Preferences>Java>Code Generation.
12 */
13 public class ControlFile {
14 //已有文件的路徑,需要備份到那個路徑的名字
15 String filePath, aimFilePath;
16 //保存已有文件路徑下的所有的文件的名字 存放String
17 Vector vec;
18
19 /* public ControlFile() {
20 filePath = "";
21 aimFilePath = "";
22 vec = new Vector();
23 }*/
24
25 public ControlFile(String filePath, String aimFilePath) {
26 this.filePath = filePath;
27 this.aimFilePath = aimFilePath;
28 vec = new Vector();
29 }
30
31 //得到目錄下所有文件的名字
32 private void getFileName() {
33 File f = new File(filePath);
34 String str[] = f.list();
35 for (int i = 0; i < str.length; i++) {
36 vec.addElement(str[i]);
37 }
38 }
39
40 // 文件的拷貝:::測試成功
41 private boolean bakFile(String fileName) {
42 try {
43 //讀文件
44 FileReader raf = new FileReader(filePath + fileName);
45 String detail = "";
46 BufferedReader buff = new BufferedReader(raf);
47 String temp = buff.readLine();
48 while (temp != null) {
49 detail += temp + "\n";
50 temp = buff.readLine();
51 }
52 raf.close();
53 System.out.println(detail);
54 //寫文件
55 File file = new File(aimFilePath + fileName);
56 PrintWriter out = new PrintWriter(new FileWriter(file));
57 out.print(detail);
58 out.close();
59
60 } catch (FileNotFoundException e) {
61 System.out.println("文件沒有找到");
62 } catch (IOException e) {
63 System.out.println("copyFile 出錯");
64 }
65 return true;
66 }
67
68 public static void main(String[] args) {
69 ControlFile confile = new ControlFile("D:\\readFile\\",
70 "D:\\work\\bakFile\\");
71 confile.getFileName();
72 Vector ve = new Vector();
73 ve = confile.vec;
74 if (ve != null)
75 for (int i = 0; i < ve.size(); i++) {
76 System.out.println((String) ve.elementAt(i));
77 confile.bakFile((String) ve.elementAt(i));
78 }
79 }
80 }
2 import java.text.*;
3 import java.util.*;
4
5 /**
6 * @author ljt
7 *
8 * To change this generated comment edit the template variable "typecomment":
9 * Window>Preferences>Java>Templates.
10 * To enable and disable the creation of type comments go to
11 * Window>Preferences>Java>Code Generation.
12 */
13 public class ControlFile {
14 //已有文件的路徑,需要備份到那個路徑的名字
15 String filePath, aimFilePath;
16 //保存已有文件路徑下的所有的文件的名字 存放String
17 Vector vec;
18
19 /* public ControlFile() {
20 filePath = "";
21 aimFilePath = "";
22 vec = new Vector();
23 }*/
24
25 public ControlFile(String filePath, String aimFilePath) {
26 this.filePath = filePath;
27 this.aimFilePath = aimFilePath;
28 vec = new Vector();
29 }
30
31 //得到目錄下所有文件的名字
32 private void getFileName() {
33 File f = new File(filePath);
34 String str[] = f.list();
35 for (int i = 0; i < str.length; i++) {
36 vec.addElement(str[i]);
37 }
38 }
39
40 // 文件的拷貝:::測試成功
41 private boolean bakFile(String fileName) {
42 try {
43 //讀文件
44 FileReader raf = new FileReader(filePath + fileName);
45 String detail = "";
46 BufferedReader buff = new BufferedReader(raf);
47 String temp = buff.readLine();
48 while (temp != null) {
49 detail += temp + "\n";
50 temp = buff.readLine();
51 }
52 raf.close();
53 System.out.println(detail);
54 //寫文件
55 File file = new File(aimFilePath + fileName);
56 PrintWriter out = new PrintWriter(new FileWriter(file));
57 out.print(detail);
58 out.close();
59
60 } catch (FileNotFoundException e) {
61 System.out.println("文件沒有找到");
62 } catch (IOException e) {
63 System.out.println("copyFile 出錯");
64 }
65 return true;
66 }
67
68 public static void main(String[] args) {
69 ControlFile confile = new ControlFile("D:\\readFile\\",
70 "D:\\work\\bakFile\\");
71 confile.getFileName();
72 Vector ve = new Vector();
73 ve = confile.vec;
74 if (ve != null)
75 for (int i = 0; i < ve.size(); i++) {
76 System.out.println((String) ve.elementAt(i));
77 confile.bakFile((String) ve.elementAt(i));
78 }
79 }
80 }