Copy the source file system structure into the supplied target location. : 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 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
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.