Example usage for java.nio.channels FileChannel transferTo

List of usage examples for java.nio.channels FileChannel transferTo

Introduction

In this page you can find the example usage for java.nio.channels FileChannel transferTo.

Prototype

public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;

Source Link

Document

Transfers bytes from this channel's file to the given writable byte channel.

Usage

From source file:org.apache.solr.util.FileUtils.java

public static void copyFile(File src, File destination) throws IOException {
    FileChannel in = null;
    FileChannel out = null;/*from   w w w  .j a v a  2  s  .c o m*/
    try {
        in = new FileInputStream(src).getChannel();
        out = new FileOutputStream(destination).getChannel();
        in.transferTo(0, in.size(), out);
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
        }
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.nuclos.common2.File.java

/**
 * Simple Method for copying files with FileChannel
 * FIX ELISA-6498/*  w  w  w. ja  v  a2 s.c  om*/
 * 
 * @param in
 * @param out
 * @throws IOException
 */
public static void copyFile(java.io.File in, java.io.File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

From source file:edu.clemson.cs.nestbed.server.util.FileUtils.java

public static void copyFile(File in, File out) throws FileNotFoundException, IOException {
    FileChannel sourceChannel = null;
    FileChannel destinationChannel = null;

    try {/*from  w ww .  j  a v  a  2s .c o  m*/
        sourceChannel = new FileInputStream(in).getChannel();
        destinationChannel = new FileOutputStream(out).getChannel();
        sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
    } finally {
        try {
            sourceChannel.close();
        } catch (Exception ex) {
        }
        try {
            destinationChannel.close();
        } catch (Exception ex) {
        }
    }
}

From source file:Main.java

/**
 * //from   w ww .  ja  va  2s  .com
 * copy file
 * 
 * @param src
 *            source file
 * @param dest
 *            target file
 * @throws IOException
 */
public static void copyFile(File src, File dest) throws IOException {
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
        if (!dest.exists()) {
            dest.createNewFile();
        }
        inChannel = new FileInputStream(src).getChannel();
        outChannel = new FileOutputStream(dest).getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:org.akubraproject.fs.FSBlob.java

private static void nioCopy(File source, File dest) throws IOException {
    FileInputStream f_in = null;/*from   w ww . j a  v  a  2 s. co  m*/
    FileOutputStream f_out = null;

    log.debug("Performing force copy-and-delete of source '" + source + "' to '" + dest + "'");
    try {
        f_in = new FileInputStream(source);

        try {
            f_out = new FileOutputStream(dest);

            FileChannel in = f_in.getChannel();
            FileChannel out = f_out.getChannel();
            in.transferTo(0, source.length(), out);
        } finally {
            IOUtils.closeQuietly(f_out);
        }
    } finally {
        IOUtils.closeQuietly(f_in);
    }

    if (!dest.exists())
        throw new IOException("Failed to copy file to new location: " + dest);
}

From source file:Main.java

@SuppressWarnings("unused")
private static boolean copy2SingleFileByChannel(File sourceFile, File destFile) {
    boolean copyOK = true;
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {// w  w  w.  ja va 2  s.co  m
        inputStream = new FileInputStream(sourceFile);
        outputStream = new FileOutputStream(destFile);
        inputChannel = inputStream.getChannel();
        outputChannel = outputStream.getChannel();
        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
    } catch (Exception e) {
        copyOK = false;
    } finally {
        try {
            inputChannel.close();
            inputStream.close();
            outputChannel.close();
            outputStream.close();
        } catch (IOException e) {
            copyOK = false;
            e.printStackTrace();
        }
    }
    return copyOK;
}

From source file:edu.unc.lib.dl.util.FileUtils.java

public static void copyFile(File in, File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {// w  w  w  .j av a 2 s .  c  om
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }

}

From source file:r2b.apps.utils.FileUtils.java

/**
 * Copy src to dst.//from   w w w  .j a  v a2 s . c om
 * @param src Source file.
 * @param dst Destination file.
 */
public static void copy(final File src, File dst) {
    try {
        FileInputStream inStream = new FileInputStream(src);
        FileOutputStream outStream = new FileOutputStream(dst);
        FileChannel inChannel = inStream.getChannel();
        FileChannel outChannel = outStream.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        inStream.close();
        outStream.close();
    } catch (IOException e) {
        Logger.e(FileUtils.class.getSimpleName(), e.toString());
    }
}

From source file:FileUtils.java

public static void copyFile(File in, File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {//  ww w.  j  a va 2 s .co  m
        // magic number for Windows, 64Mb - 32Kb)
        int maxCount = (64 * 1024 * 1024) - (32 * 1024);
        long size = inChannel.size();
        long position = 0;
        while (position < size) {
            position += inChannel.transferTo(position, maxCount, outChannel);
        }
    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

From source file:Main.java

public static File copyFileTo(File src, File dst) {
    FileChannel in = null;
    FileChannel out = null;//from ww w  .  j  av a  2 s.c  o m
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    try {
        inStream = new FileInputStream(src);
        outStream = new FileOutputStream(dst);
        in = inStream.getChannel();
        out = outStream.getChannel();
        in.transferTo(0, in.size(), out);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            if (inStream != null) {
                inStream.close();
            }
            if (outStream != null) {
                outStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return dst;
}