程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java實現的類似windows操作系統的xCopy

java實現的類似windows操作系統的xCopy

編輯:關於JAVA

package com.iwindyforest.dir;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class FileSystem
{
  private ArrayList<String> fileList = new ArrayList<String>();

  public FileSystem(String path)
  {
    long a = System.currentTimeMillis();
    this.listFiles(path);
    this.print("TimeCost:" + (System.currentTimeMillis() - a) + " Millis");

    this.xCopy(path, "C: \\temp");
  }

  private void print(String message)
  {
    System.out.println(message);
  }

  public void listFiles(String strPath)
  {
    File dir = new File(strPath);
    if(dir != null && dir.exists())
    {
      if(dir.isDirectory())
      {
        File[] files;

        try
        {
          files = dir.listFiles();
        }
        catch(SecurityException e)
        {
          files = null;
          e.printStackTrace();
        }

        if(files == null)
        {
          return;
        }
        else
        {
          for(int i = 0; i < files.length; i++)
          {
            String strFileName = files[i].getAbsolutePath();
            if(files[i].isDirectory())
            {
              this.print("D--:" + strFileName);
              this.listFiles(files[i].getAbsolutePath());
            }
            else
            {
              this.print("F--:" + strFileName);
              fileList.add(files[i].getAbsolutePath());
            }
          }
        }
      }
      else
      {
        this.print("F--:" + dir.getAbsolutePath());
      }
    }
    else
    {
      this.print("FileNotExist:" + dir.getAbsolutePath());
    }
  }

  private boolean checkDir(File dir)
  {

    if(dir == null)
    {
      this.print("dirPath is null");
      return false;
    }
    else if(!dir.exists())
    {
      this.print("dirPath: " + dir.getAbsolutePath() + " doesn't exist.");
      return false;
    }
    else if(!dir.isDirectory())
    {
      this.print("dirPath: " + dir.getAbsolutePath()
          + " is not a directory.");
      return false;
    }
    else
    {
      return true;
    }
  }

  /**
   * 類似與windows操作系統的xCopy,遞歸拷貝整個源目錄到目標目錄。源目錄和目標目錄必須已經存在。
   *
   * @param srcDirPath
   * @param destDirPath
   */
  public void xCopy(String srcDirPath, String destDirPath)
  {
    File srcDir = new File(srcDirPath);
    File destDir = new File(destDirPath);
    if(this.checkDir(srcDir) && this.checkDir(destDir))
    {
      File[] files;

      try
      {
        files = srcDir.listFiles();
      }
      catch(SecurityException e)
      {
        files = null;
        this.print("xCopy breaked: can't listFiles,may be caused by:");
        e.printStackTrace();
        return;
      }

      if(files == null)
      {
        return;
      }
      else
      {
        for(int i = 0; i < files.length; i++)
        {
          String fileName = files[i].getName();
          String absoluteFileName = files[i].getAbsolutePath();

          if(files[i].isDirectory())
          {
            // 下一次遞歸的源目錄
              String subSrcDir = srcDir.getPath() + File.separator
                + fileName;

            // 下一次遞歸的目的目錄
              String subDestDir = destDir.getPath() + File.separator
                + fileName;

            try
            {
              new File(subDestDir).mkdir();
            }
            catch(SecurityException e)
            {
              this.print("can't mkdir in path : " + subDestDir);
              this.print("xCopy breaked cause by: ");
              e.printStackTrace();
              return;
            }

            xCopy(subSrcDir, subDestDir);
          }
          else
          {

            String destFileName = destDirPath + File.separator
                + fileName;
            copyFile(absoluteFileName, destFileName);
          }
        }
      }
    }
  }

  /**
   * 簡單復制單個文件到目標路徑。目標路徑下的該文件必須有可寫權限
   *
   * @param srcFilePath
   * @param desFilePath
   */
  public void copyFile(String srcFilePath, String desFilePath)
  {
    int byteread = 0;
    InputStream in = null;
    FileOutputStream out = null;

    try
    {
      in = new FileInputStream(srcFilePath);
      out = new FileOutputStream(desFilePath);
    }
    catch(FileNotFoundException e)
    {
      e.printStackTrace();
    }

    byte[] buffer = new byte[1024];

    try
    {
      while((byteread = in.read(buffer)) != -1)
      {
        out.write(buffer, 0, byteread);
      }

      in.close();
      out.close();
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
  }

  public static void main(String[] args)
  {
    if(args.length == 1)
    {
      new FileSystem(args[0]);
    }
    else
    {
      new FileSystem(System.getProperty("user.dir", "c:\\"));
    }
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved