Java FileChannel Copy copyFile(File sourceFile, File targetFile)

Here you can find the source of copyFile(File sourceFile, File targetFile)

Description

copy File

License

LGPL

Declaration

public static void copyFile(File sourceFile, File targetFile) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w ww .  ja va 2  s .  co m*/
 * GrGen: graph rewrite generator tool -- release GrGen.NET 4.4
 * Copyright (C) 2003-2015 Universitaet Karlsruhe, Institut fuer Programmstrukturen und Datenorganisation, LS Goos; and free programmers
 * licensed under LGPL v3 (see LICENSE.txt included in the packaging of this file)
 * www.grgen.net
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    public static void copyFile(File sourceFile, File targetFile) throws IOException {
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }

        FileChannel sourceStream = null;
        FileChannel targetStream = null;
        try {
            sourceStream = new FileInputStream(sourceFile).getChannel();
            targetStream = new FileOutputStream(targetFile).getChannel();

            long count = 0;
            long size = sourceStream.size();
            while ((count += targetStream.transferFrom(sourceStream, count, size - count)) < size)
                ;
        } finally {
            if (sourceStream != null)
                sourceStream.close();
            if (targetStream != null)
                targetStream.close();
        }
    }
}

Related

  1. copyFile(File sourceFile, File destFile)
  2. copyFile(File sourceFile, File destFile)
  3. copyFile(File sourceFile, File destinationFile)
  4. copyFile(File sourceFile, File destinationFile)
  5. copyFile(File sourceFile, File destinationFile)
  6. copyFile(File sourceFile, File targetFile)
  7. copyFile(File sourceFile, File targetFile)
  8. copyFile(File src, File dest)
  9. copyFile(File src, File dest)