Copy the source file system structure into the supplied target location. : Copy « File « Java Tutorial






/*
 * JBoss DNA (http://www.jboss.org/dna)
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * See the AUTHORS.txt file in the distribution for a full listing of 
 * individual contributors. 
 *
 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
 * is licensed to you under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * JBoss DNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileMonitor {

  /**
   * Copy the source file system structure into the supplied target location. If
   * the source is a file, the destiniation will be created as a file; if the
   * source is a directory, the destination will be created as a directory.
   * 
   * @param sourceFileOrDirectory
   *          the file or directory whose contents are to be copied into the
   *          target location
   * @param destinationFileOrDirectory
   *          the location where the copy is to be placed; does not need to
   *          exist, but if it does its type must match that of <code>src</code>
   * @return the number of files (not directories) that were copied
   * @throws IllegalArgumentException
   *           if the <code>src</code> or <code>dest</code> references are
   *           null
   * @throws IOException
   */
  public static int copy(File sourceFileOrDirectory, File destinationFileOrDirectory)
      throws IOException {
    int numberOfFilesCopied = 0;
    if (sourceFileOrDirectory.isDirectory()) {
      destinationFileOrDirectory.mkdirs();
      String list[] = sourceFileOrDirectory.list();

      for (int i = 0; i < list.length; i++) {
        String dest1 = destinationFileOrDirectory.getPath() + File.separator + list[i];
        String src1 = sourceFileOrDirectory.getPath() + File.separator + list[i];
        numberOfFilesCopied += copy(new File(src1), new File(dest1));
      }
    } else {
      InputStream fin = new FileInputStream(sourceFileOrDirectory);
      fin = new BufferedInputStream(fin);
      try {
        OutputStream fout = new FileOutputStream(destinationFileOrDirectory);
        fout = new BufferedOutputStream(fout);
        try {
          int c;
          while ((c = fin.read()) >= 0) {
            fout.write(c);
          }
        } finally {
          fout.close();
        }
      } finally {
        fin.close();
      }
      numberOfFilesCopied++;
    }
    return numberOfFilesCopied;
  }
}








11.78.Copy
11.78.1.Copy the source file system structure into the supplied target location.
11.78.2.Buffered copying between source(InputStream, Reader, String and byte[]) and destinations (OutputStream, Writer, String and byte[]).
11.78.3.Copies all data from an input stream to an output stream.
11.78.4.Copies the contents of the Reader into the Writer, until the end of the stream has been reached.
11.78.5.Copy a directory and all of its contents.
11.78.6.Copy a file and user buffer
11.78.7.Copy chars from a Reader to a Writer.
11.78.8.copy Completely (InputStream input, OutputStream output)
11.78.9.copy Completely (Reader input, Writer output)
11.78.10.copy Completely(URI input, URI output)
11.78.11.Buffered copying
11.78.12.Copy file and directory