Java FileChannel Copy copyFile(File src, File target)

Here you can find the source of copyFile(File src, File target)

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(File src, File target) 

Method Source Code

//package com.java2s;
/*/*from  w ww. j a  v a  2  s.co m*/
 * JFileHelper.java
 *
 * Created on 28. Mai 2006, 13:06
 *
 *  Copyright (C) 28. Mai 2006  <reiner>
    
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.io.IOException;

public class Main {
    public static boolean copyFile(File src, File target) {
        boolean flag = false;
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(target);
            FileChannel inCh = in.getChannel();
            FileChannel outCh = out.getChannel();
            if (outCh.transferFrom(inCh, 0, src.length()) == src.length()) {
                // adjust timestamp
                target.setLastModified(src.lastModified());
                flag = true;
            }
        } catch (IOException ex) {
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException ex) {
            }
            try {
                if (out != null)
                    out.close();
            } catch (IOException ex) {
            }
        }
        return flag;
    }
}

Related

  1. copyFile(File src, File dst)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File dst)
  5. copyFile(File src, File target)
  6. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  7. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  8. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  9. copyFile(File srcFile, File dstFile, boolean overwrite)