Copy a directory and all of its contents. : Copy « File Input Output « Java






Copy a directory and all of its contents.

  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*

 Derby - Class org.apache.derby.iapi.util.PropertyUtil

 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to you under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 */

public class Main {
  private static final int BUFFER_SIZE = 4096 * 4;

  /**
   * Copy a directory and all of its contents.
   */
  public static boolean copyDirectory(File from, File to) {
    return copyDirectory(from, to, (byte[]) null, (String[]) null);
  }

  public static boolean copyDirectory(String from, String to) {
    return copyDirectory(new File(from), new File(to));
  }

  /**
   * @param filter -
   *          array of names to not copy.
   */
  public static boolean copyDirectory(File from, File to, byte[] buffer, String[] filter) {
    //
    // System.out.println("copyDirectory("+from+","+to+")");

    if (from == null)
      return false;
    if (!from.exists())
      return true;
    if (!from.isDirectory())
      return false;

    if (to.exists()) {
      // System.out.println(to + " exists");
      return false;
    }
    if (!to.mkdirs()) {
      // System.out.println("can't make" + to);
      return false;
    }

    String[] list = from.list();

    // Some JVMs return null for File.list() when the
    // directory is empty.
    if (list != null) {

      if (buffer == null)
        buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files

      nextFile: for (int i = 0; i < list.length; i++) {

        String fileName = list[i];

        if (filter != null) {
          for (int j = 0; j < filter.length; j++) {
            if (fileName.equals(filter[j]))
              continue nextFile;
          }
        }

        File entry = new File(from, fileName);

        // System.out.println("\tcopying entry " + entry);

        if (entry.isDirectory()) {
          if (!copyDirectory(entry, new File(to, fileName), buffer, filter))
            return false;
        } else {
          if (!copyFile(entry, new File(to, fileName), buffer))
            return false;
        }
      }
    }
    return true;
  }

  public static boolean copyFile(File from, File to) {
    return copyFile(from, to, (byte[]) null);
  }

  public static boolean copyFile(File from, File to, byte[] buf) {
    if (buf == null)
      buf = new byte[BUFFER_SIZE];

    //
    // System.out.println("Copy file ("+from+","+to+")");
    FileInputStream from_s = null;
    FileOutputStream to_s = null;

    try {
      from_s = new FileInputStream(from);
      to_s = new FileOutputStream(to);

      for (int bytesRead = from_s.read(buf); bytesRead != -1; bytesRead = from_s.read(buf))
        to_s.write(buf, 0, bytesRead);

      from_s.close();
      from_s = null;

      to_s.getFD().sync(); // RESOLVE: sync or no sync?
      to_s.close();
      to_s = null;
    } catch (IOException ioe) {
      return false;
    } finally {
      if (from_s != null) {
        try {
          from_s.close();
        } catch (IOException ioe) {
        }
      }
      if (to_s != null) {
        try {
          to_s.close();
        } catch (IOException ioe) {
        }
      }
    }

    return true;
  }
}

   
    
  








Related examples in the same category

1.Copies file contents from source to destination
2.Copies the contents of the given InputStream to the given OutputStream
3.Copy Pipe
4.Copy any input stream to output file
5.Copy any input stream to output stream
6.Copy file and directory
7.Utility methods for file and stream copying
8.Copies all data from an input stream to an output stream.
9.copy Completely (InputStream input, OutputStream output)
10.copy Completely (Reader input, Writer output)
11.copy Completely(URI input, URI output)
12.Copies the InputStream into the OutputStream, until the end of the stream has been reached.
13.Copies the InputStream into the OutputStream, until the end of the stream has been reached. This method uses a buffer of 4096 kbyte.
14.Copies the contents of the Reader into the Writer, until the end of the stream has been reached.
15.Copies the contents of the Reader into the Writer, until the end of the stream has been reached. This method uses a buffer of 4096 kbyte.
16.Copy a file and user buffer
17.Copy chars from a Reader to a Writer.
18.Copy in stream to an out stream
19.Copy the source file system structure into the supplied target location.