Java Copy Directory copyDirectory(File srcDir, File dstDir)

Here you can find the source of copyDirectory(File srcDir, File dstDir)

Description

Copies source directory to destination directory.

License

Open Source License

Parameter

Parameter Description
srcDir source directory. The contents of this directory (not the directory itself) are copied to the dstDir.
dstDir destination directory. The contents of srcDir will be written to dstDir.

Exception

Parameter Description
IOException an exception

Declaration

public static void copyDirectory(File srcDir, File dstDir) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    /**//  w ww. j  a v a  2s  . c o m
     * Copies source directory to destination directory. Does not delete the destination directory or any of its contents, although it will overwrite files with the same name.
     * @param srcDir source directory. The contents of this directory (not the directory itself) are copied to the dstDir.
     * @param dstDir destination directory. The contents of srcDir will be written to dstDir.
     * @throws IOException
     */
    public static void copyDirectory(File srcDir, File dstDir) throws IOException {
        if (srcDir.isDirectory()) {
            if (!dstDir.exists()) {
                dstDir.mkdir();
            }

            String[] children = srcDir.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(srcDir, children[i]), new File(dstDir, children[i]));
            }
        } else {
            copyFile(srcDir, dstDir);
        }
    }

    public static void copyFile(File src, File dest) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);
        try {
            writeInputToOutput(in, out, 8192);
        } finally {
            in.close();
            out.close();
        }
    }

    /** Reads bytes from an InputStream and transfers them to an OutputStream.
     * It will block and keep reading until the InputStream is exhausted and returns a -1 on read().
     * It will flush() the OutputStream, but it does NOT close the InputStream or OutputStream.
     * @param bufferSize The size of the chunks that will be used for the transfer. Large buffers take more memory, but are generally more efficient. Use IOUtils.CHUNK_SIZE when in doubt.
     * @param in The stream to read from.
     * @param out The stream to write to.
     * @return The actual number of bytes transferred.
     * @throws java.io.IOException
     */
    static public long writeInputToOutput(InputStream in, OutputStream out, int bufferSize) throws IOException {
        if (in == null)
            throw new IllegalArgumentException("You cannot pass a null InputStream");
        if (out == null)
            throw new IllegalArgumentException("You cannot pass a null OutputStream");
        byte[] buffer = new byte[bufferSize];
        int lastBytesRead;
        long totalBytesRead = 0;
        while ((lastBytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, lastBytesRead);
            totalBytesRead += lastBytesRead;
        }
        out.flush();
        return totalBytesRead;
    }

    /**
     * Reads an InputStream and writes it to an OutputStream. Stops after encountering stopSequence. This reads and writes a single byte at a time, so it is a good idea for performance purposes to use buffered streams.
     * @param in The InputStream to read. Must be non-null.
     * @param out the OutputStream to write to. If null, then the input will still be scanned, but nothing will be written.
     * @param stopSequence Reading will stop after encountering this byte sequence.
     * @return The total number of bytes read.
     * @throws IOException
     */
    static public long writeInputToOutput(InputStream in, OutputStream out, byte[] stopSequence)
            throws IOException {
        int result = 0;
        int sequenceMark = 0;
        int sequenceLength = stopSequence.length;
        int eachByte;
        while ((sequenceMark < sequenceLength) && (result < 1600)) { //FIX! Temporary stopgap to prevent crash; stop after reading 1600 bytes
            eachByte = in.read();
            if (eachByte == -1)
                break;
            //System.out.print( (char)eachByte );
            out.write(eachByte);
            if (eachByte == stopSequence[sequenceMark])
                sequenceMark++;
            else
                sequenceMark = 0;
            result++;
        }
        return result;
    }
}

Related

  1. copyDirectory(File src, File dest, boolean force)
  2. copyDirectory(File src, File dst)
  3. copyDirectory(File srcDir, File destDir)
  4. copyDirectory(File srcDir, File dstDir)
  5. copyDirectory(File srcDir, File dstDir)
  6. copyDirectory(File srcDir, File dstDir)
  7. copyDirectory(File srcDir, File dstDir)
  8. copyDirectory(File srcDir, File dstDir)
  9. copyDirectory(File srcFile, File destFile)