天空是藍色的

          做好軟件為中國 #gcc -c helloworld.c -o helloworld.o //編譯目標文件 #gcc helloworld.o -o helloworld //編譯成可執行exe #helloworld //運行exe
          數據加載中……
          [轉載]文件和目錄復制函數
          http://spaces.msn.com/members/kliweik/

          今天偷懶,要Roy幫我寫了一個目錄復制的函數,記錄,備后用,呵呵!

          /* Created on 2005-8-8 
           * 
           * @author wei.li  
           * TODO 用來運行外部命令的工具類 */
          package com.linktone.photo.lucene
          ;

          import java.io.File
          ;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.nio.channels.FileChannel;

          public class FileUtil {
              /** * 功能:利用nio來快速復制文件 */
              public static void copyFile(String srcFile
          , String destFile)
                      throws java.io.FileNotFoundException
          , java.io.IOException {
                  FileInputStream fis 
          = new FileInputStream(srcFile);
                  FileOutputStream fos = new FileOutputStream(destFile);
                  FileChannel fcin = fis.getChannel();
                  FileChannel fcout = fos.getChannel();
                  fcin.transferTo(0, fcin.size(), fcout);
                  fcin.close();
                  fcout.close();
                  fis.close();
                  fos.close();
              }

              /** * 功能:利用nio快速復制目錄 */
              public static void copyDirectory(String srcDirectory
          , String destDirectory)
                      throws java.io.FileNotFoundException
          , java.io.IOException { // 得到目錄下的文件和目錄數組
                  File srcDir 
          = new File(srcDirectory);
                  File[] fileList = srcDir.listFiles();
                  // 循環處理數組
                  for (int i 
          = 0; i < fileList.length; i++) {
                      if (fileList[i].isFile()) {
                          // 數組中的對象為文件
                          // 如果目標目錄不存在,創建目標目錄
                          File descDir 
          = new File(destDirectory);
                          if (!descDir.exists()) {
                              descDir.mkdir()
          ;
                          } // 復制文件到目標目錄
                          copyFile(srcDirectory + 
          "/" + fileList[i].getName(),
                                  destDirectory + 
          "/" + fileList[i].getName());
                      } else {
                          // 數組中的對象為目錄
                          // 如果該子目錄不存在就創建(其中也包含了對多級目錄的處理)
                          File subDir 
          = new File(destDirectory + "/"
                                  + fileList
          [i].getName());
                          if (!subDir.exists()) {
                              subDir.mkdir()
          ;
                          }
                          // 遞歸處理子目錄
                          copyDirectory(srcDirectory + 
          "/" + fileList[i].getName(),
                                  destDirectory + 
          "/" + fileList[i].getName());

                      }
                  }
              }

              public static void main(String
          [] args) throws IOException {

                  FileUtil.copyDirectory(
          "e:/temp/index/test", "e:/temp/index/test2");
              }

          }

          posted on 2005-12-29 17:52 bluesky 閱讀(740) 評論(0)  編輯  收藏 所屬分類: 工作總結

          主站蜘蛛池模板: 贡觉县| 凤山县| 汽车| 绥中县| 连云港市| 邻水| 崇义县| 井研县| 浑源县| 静安区| 建瓯市| 邹城市| 名山县| 中江县| 策勒县| 积石山| 子长县| 奇台县| 张掖市| 班玛县| 祁东县| 安阳市| 阿拉善盟| 屏东市| 蒙山县| 镶黄旗| 通化市| 永宁县| 贵州省| 昌平区| 陆川县| 偏关县| 师宗县| 彩票| 邳州市| 临澧县| 思茅市| 定西市| 靖安县| 德州市| 义马市|