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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » CopyScreenshots 
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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.