Example usage for java.nio.channels FileChannel size

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

Introduction

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

Prototype

public abstract long size() throws IOException;

Source Link

Document

Returns the current size of this channel's file.

Usage

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        if (!destFile.createNewFile()) {
            throw new IOException("Cannot create file: " + destFile.getCanonicalFile());
        }//w w w .j ava  2 s . c o m
    }
    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        long count = 0;
        long size = source.size();
        while ((count += destination.transferFrom(source, count, size - count)) < size)
            ;
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:Main.java

/**
 * Copy a file by using the file channels, it could be 10x faster than the traditional way of
 * copying file by using the file streams.
 * /*from  w  w w.j  a va  2s  . c o  m*/
 * @param src     the source file
 * @param dest    the destination file
 * @throws IOException
 */
public static void copyFileUsingFileChannels(File src, File dest) throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;

    try {
        inputChannel = new FileInputStream(src).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } finally {
        inputChannel.close();
        outputChannel.close();
    }
}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {

    new File(destFile.getParent()).mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();//  ww  w . ja  va 2  s. c o m
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.getParentFile().exists())
        destFile.getParentFile().mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();// w  ww  . j a v  a2  s . c o m
    } else {
        destFile.delete();
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:Main.java

/**
 * Copy a file./*from w  w w  .j  a va 2  s.  co m*/
 * 
 * @param sourceFile
 *            The source
 * @param destDir
 *            The destination directory
 * @throws IOException
 *             Everything fails sometimes
 */
protected static void CopyFileToDir(File sourceFile, File destDir) throws IOException {

    File destFile = new File(destDir, sourceFile.getName());

    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();/*ww w .  j a va  2  s  .  c om*/
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        closeQuietly(source);
        closeQuietly(destination);
    }
}

From source file:com.github.neoio.nio.util.NIOUtils.java

public static long fileSize(FileChannel channel) throws NetIOException {
    try {/*from ww w .j a  v  a 2s.  c o m*/
        return channel.size();
    } catch (IOException e) {
        throw new NetIOException("Error getting file size", e);
    }
}

From source file:Main.java

/**
 * Copy file using NOI//w w w .j ava2s  .co  m
 *
 * @param source
 * @param dest
 */
public static void copyFile(String source, String dest) throws IOException {
    File sourceFile = new File(source);
    File destFile = new File(dest);
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
        inputChannel = new FileInputStream(sourceFile).getChannel();
        outputChannel = new FileOutputStream(destFile).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } finally {
        try {
            inputChannel.close();
            outputChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.cats.version.utils.Utils.java

public static void copyFile(File srcFile, File destFile) {
    FileInputStream fis = null;/*  www  .  jav  a 2  s  . co m*/
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);
        FileChannel inChannel = fis.getChannel();
        FileChannel outChannel = fos.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        Utils.closeRes(fis);
        Utils.closeRes(fos);
    }
}

From source file:VASSAL.tools.io.IOUtils.java

/**
 * Copies bytes from a large (over 2GB) <code>FileInputStream</code> to a
 * <code>FileOutputStream</code>.
 *
 * This method uses channels. The input file should not be written
 * to during the copy.// www.  j  a  va2 s.co m
 *
 * @param in the source
 * @param out the destination
 * @throws IOException if one occurs while reading or writing
 */
public static long copyLarge(FileInputStream in, FileOutputStream out) throws IOException {
    final FileChannel inc = in.getChannel();
    return inc.transferTo(0L, inc.size(), out.getChannel());
}