天空是藍(lán)色的

          做好軟件為中國 #gcc -c helloworld.c -o helloworld.o //編譯目標(biāo)文件 #gcc helloworld.o -o helloworld //編譯成可執(zhí)行exe #helloworld //運行exe
          數(shù)據(jù)加載中……
          [轉(zhuǎn)載]文件和目錄復(fù)制函數(shù)
          http://spaces.msn.com/members/kliweik/

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

          /* 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來快速復(fù)制文件 */
              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快速復(fù)制目錄 */
              public static void copyDirectory(String srcDirectory
          , String destDirectory)
                      throws java.io.FileNotFoundException
          , java.io.IOException { // 得到目錄下的文件和目錄數(shù)組
                  File srcDir 
          = new File(srcDirectory);
                  File[] fileList = srcDir.listFiles();
                  // 循環(huán)處理數(shù)組
                  for (int i 
          = 0; i < fileList.length; i++) {
                      if (fileList[i].isFile()) {
                          // 數(shù)組中的對象為文件
                          // 如果目標(biāo)目錄不存在,創(chuàng)建目標(biāo)目錄
                          File descDir 
          = new File(destDirectory);
                          if (!descDir.exists()) {
                              descDir.mkdir()
          ;
                          } // 復(fù)制文件到目標(biāo)目錄
                          copyFile(srcDirectory + 
          "/" + fileList[i].getName(),
                                  destDirectory + 
          "/" + fileList[i].getName());
                      } else {
                          // 數(shù)組中的對象為目錄
                          // 如果該子目錄不存在就創(chuàng)建(其中也包含了對多級目錄的處理)
                          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)  編輯  收藏 所屬分類: 工作總結(jié)

          主站蜘蛛池模板: 大埔区| 揭阳市| 海安县| 闻喜县| 安新县| 册亨县| 扬中市| 邹城市| 蛟河市| 福清市| 龙井市| 伊春市| 建瓯市| 漳平市| 五原县| 班玛县| 铜川市| 花莲县| 鄂尔多斯市| 萝北县| 房山区| 简阳市| 泗水县| 阿瓦提县| 平遥县| 西乌| 乐平市| 印江| 瑞安市| 怀来县| 安丘市| 若尔盖县| 磐石市| 青田县| 岳阳县| 白水县| 定襄县| 通榆县| 梅河口市| 郯城县| 阳朔县|