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 閱讀(1530) 評論(1)  編輯  收藏

          評論

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

          知道
            回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2008年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          統計

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 巴彦县| 台前县| 怀柔区| 加查县| 彭州市| 河间市| 轮台县| 济南市| 东城区| 东海县| 灵川县| 汾西县| 宾川县| 湟中县| 赣榆县| 绥化市| 崇义县| 桂东县| 甘洛县| 青川县| 黄梅县| 常宁市| 胶南市| 抚顺县| 固安县| 石楼县| 黄陵县| 新宁县| 遵化市| 疏勒县| 尼玛县| 南靖县| 巴林左旗| 南投县| 木兰县| 张掖市| 南安市| 黔江区| 扎兰屯市| 乌鲁木齐市| 海丰县|