Java File Copy nio copy(File toFile, File fromFile)

Here you can find the source of copy(File toFile, File fromFile)

Description

Copies data from one file to another.

License

Open Source License

Parameter

Parameter Description
toFile the file to which data will be written
fromFile the file from which data will be read

Exception

Parameter Description
IOException if an I/O error occurs

Declaration

public static void copy(File toFile, File fromFile) throws IOException 

Method Source Code


//package com.java2s;
/*/*from ww w .j av a2s  .co  m*/
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the JBPM BPEL PUBLIC LICENSE AGREEMENT as
 * published by JBoss Inc.; either version 1.0 of the License, or
 * (at your option) any later version.
 *
 * This software 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.
 */

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

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copies data from one file to another.
     * @param toFile the file to which data will be written
     * @param fromFile the file from which data will be read
     * @throws IOException if an I/O error occurs
     */
    public static void copy(File toFile, File fromFile) throws IOException {
        FileChannel source = new FileInputStream(fromFile).getChannel();
        try {
            FileChannel sink = new FileOutputStream(toFile).getChannel();
            try {
                for (long position = 0, size = source.size(); position < size;)
                    position += source.transferTo(position, size - position, sink);
            } finally {
                sink.close();
            }
        } finally {
            source.close();
        }
    }

    /**
     * Copies files that satisfy the specified filter from one directory to another.
     * @param toDir the directory to which files will be written
     * @param fromDir the directory from which files will be read
     * @param filter a file filter
     * @throws IOException if an I/O error occurs
     */
    public static void copy(File toDir, File fromDir, FileFilter filter) throws IOException {
        if (!toDir.exists() && !toDir.mkdir())
            throw new IOException("directory making failed: " + toDir);

        File[] files = fromDir.listFiles(filter);
        if (files == null)
            throw new IOException("file listing failed: " + fromDir);

        // copy files in the directory
        for (int i = 0; i < files.length; i++) {
            File fromFile = files[i];
            File toFile = new File(toDir, fromFile.getName());

            if (fromFile.isDirectory())
                copy(toFile, fromFile, filter);
            else
                copy(toFile, fromFile);
        }
    }
}

Related

  1. copy(File src, File target)
  2. copy(File srcDir, File relSrcFile, File destDir, File relDestFile)
  3. copy(File srcfile, File destfile)
  4. copy(File srcFile, File dstFile)
  5. copy(File srcFileOrDir, File targetFileOrDir)
  6. copyFile(File f1, File f2)
  7. copyFile(File source, File dest, boolean visibleFilesOnly)
  8. copyFile(File src, File dest)
  9. copyFile(FileChannel srcFc, File dstFile)