天空是藍色的

          做好軟件為中國 #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 閱讀(745) 評論(0)  編輯  收藏 所屬分類: 工作總結

          主站蜘蛛池模板: 永川市| 博野县| 崇礼县| 成安县| 六枝特区| 商南县| 闽侯县| 华亭县| 兴国县| 琼海市| 星座| 柳河县| 鹤壁市| 渑池县| 高要市| 棋牌| 鄂伦春自治旗| 嵊泗县| 沈丘县| 潜山县| 东辽县| 左云县| 临泽县| 靖西县| 舟山市| 抚顺市| 务川| 舞阳县| 襄垣县| 陆川县| 五大连池市| 余干县| 特克斯县| 广汉市| 南和县| 邹城市| 葫芦岛市| 宜阳县| 岳普湖县| 高密市| 邵阳县|