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






Copy the source file system structure into the supplied target location.

 
/*
 * 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;
  }
}

   
  








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 a directory and all of its contents.
18.Copy chars from a Reader to a Writer.
19.Copy in stream to an out stream