Java File Copy copyFiles(File fromDir, File toDir)

Here you can find the source of copyFiles(File fromDir, File toDir)

Description

Copies all files in fromDir to toDir.

License

Open Source License

Declaration

public static void copyFiles(File fromDir, File toDir)
        throws IOException 

Method Source Code

//package com.java2s;
/*-//from  w ww.j  a va2 s .  co m
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002, 2011 Oracle and/or its affiliates.  All rights reserved.
 *
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * Copies all files in fromDir to toDir.  Does not copy subdirectories.
     */
    public static void copyFiles(File fromDir, File toDir)
            throws IOException {

        String[] names = fromDir.list();
        if (names != null) {
            for (int i = 0; i < names.length; i += 1) {
                File fromFile = new File(fromDir, names[i]);
                if (fromFile.isDirectory()) {
                    continue;
                }
                File toFile = new File(toDir, names[i]);
                int len = (int) fromFile.length();
                byte[] data = new byte[len];
                FileInputStream fis = null;
                FileOutputStream fos = null;
                try {
                    fis = new FileInputStream(fromFile);
                    fos = new FileOutputStream(toFile);
                    fis.read(data);
                    fos.write(data);
                } finally {
                    if (fis != null) {
                        fis.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                }
            }
        }
    }
}

Related

  1. copy(File from, File to)
  2. copyFile(File source, File target)
  3. copyFile(File src, File dest, Component parentComponent, String message)
  4. copyFileFromFileSystem(String sourceFileName, File target)
  5. copyFiles(File file, File destPath)
  6. copyFiles(File source, File destination)
  7. copyFiles(File sourceFile, File targetDir)
  8. copyFiles(File src, File dest)
  9. copyFiles(File src, File dest)