Copy a file and user buffer : Copy « File Input Output « Java

Home
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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » File Input Output » Copy 




Copy a file and user buffer
  
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 directory and all of its contents.
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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.