java建立新文件夹和实现文件的拷贝

 

 1import java.io.*;
 2
 3public 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

 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 }

posted on 2008-09-18 10:11 anoic 阅读(1505) 评论(1)  编辑  收藏

评论

# 新文件夹 2008-12-26 17:57 新文件夹

知道
  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(3)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜