Example usage for java.nio.channels FileChannel close

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

Introduction

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

Prototype

public final void close() throws IOException 

Source Link

Document

Closes this channel.

Usage

From source file:Main.java

public static void copyFileUsingFileChannels(File source, File dest) throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {/*from  w ww.  ja v a2s  .  c  o  m*/
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } finally {
        inputChannel.close();
        outputChannel.close();
    }
}

From source file:Main.java

public static ByteBuffer fromFile(File file) throws IOException {
    RandomAccessFile raf = null;//from  w ww.j  av a2s  .  com
    FileChannel channel = null;
    try {
        raf = new RandomAccessFile(file, "r");
        channel = raf.getChannel();
        return channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()).load();
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                // Ignored.
            }
        }
        if (raf != null) {
            try {
                raf.close();
            } catch (IOException e) {
                // Ignored.
            }
        }
    }
}

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  av  a2s .  com*/
 * @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

private static void writeFileFromBytes(final File file, final byte[] bytes) {
    FileChannel fc = null;
    try {/*  ww w .  j a v  a 2 s  . c  o m*/
        fc = new FileOutputStream(file, false).getChannel();
        fc.write(ByteBuffer.wrap(bytes));
        fc.force(true);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fc != null) {
                fc.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
    FileChannel fromChannel = null;
    FileChannel toChannel = null;
    try {/*w w w  .ja va2s  . c om*/
        fromChannel = fromFile.getChannel();
        toChannel = toFile.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        try {
            if (fromChannel != null) {
                fromChannel.close();
            }
        } finally {
            if (toChannel != null) {
                toChannel.close();
            }
        }
    }
}

From source file:Main.java

private static void copy(File srcFile, File targetFile) throws IOException {
    FileChannel src = null, dst = null;
    try {// ww  w.  ja  v  a  2 s  .  c o m
        if (srcFile.exists() && (!targetFile.exists() || targetFile.delete())) {
            src = new FileInputStream(srcFile).getChannel();
            dst = new FileOutputStream(targetFile).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    } finally {
        try {
            if (src != null)
                src.close();
        } catch (IOException e) {
            Log.e("DIARY", "Failed to close resource.", e);
        }
        try {
            if (dst != null)
                src.close();
        } catch (IOException e) {
            Log.e("DIARY", "Failed to close resource.", e);
        }
    }
}

From source file:org.eclipse.smila.binarystorage.persistence.io.BssIOUtils.java

/**
 * Utility method to close a file channel.
 * //w w  w.ja va  2s.  c  om
 * @param channel
 * @throws BinaryStorageException
 */
private static void closeChannel(final FileChannel channel) throws BinaryStorageException {
    if (channel != null) {
        try {
            channel.close();
        } catch (final IOException exception) {
            throw new BinaryStorageException(exception, "Could not close stream channel.");
        }
    }
}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();// w ww. ja v  a 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());
    } 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();/* ww  w  .j av  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

private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {/*from  w  ww.  j  a v  a 2 s.co  m*/
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } finally {
        if (inputChannel != null)
            inputChannel.close();
        if (outputChannel != null)
            outputChannel.close();
    }
}