集册 Java实例教程 复制目录

复制目录

欢马劈雪     最近更新时间:2020-01-02 10:19:05

495
复制目录

import java.io.File;

import java.io.FileInputStream;//NowJava.com 提 供

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;


public class Main {

  public void copyDirectory(File srcDir, File dstDir) throws IOException {

    if (srcDir.isDirectory()) {

      if (!dstDir.exists()) {

        dstDir.mkdir();

      }


      String[] children = srcDir.list();

      for (int i = 0; i < children.length; i++) {
      /*
       from n o w  j a v a  . c o m 
      */

        copyDirectory(new File(srcDir, children[i]), new File(dstDir,

            children[i]));

      }

    } else {

      // This method is implemented in Copying a File

      copyFile(srcDir, dstDir);

    }

  }


  void copyFile(File src, File dst) throws IOException {

    InputStream in = new FileInputStream(src);

    OutputStream out = new FileOutputStream(dst);


    
展开阅读全文