Example usage for java.io FileOutputStream getChannel

List of usage examples for java.io FileOutputStream getChannel

Introduction

In this page you can find the example usage for java.io FileOutputStream getChannel.

Prototype

public FileChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.FileChannel FileChannel object associated with this file output stream.

Usage

From source file:com.zoffcc.applications.zanavi.Navit.java

private void export_map_points_to_sdcard() {
    String orig_file = NAVIT_DATA_SHARE_DIR + Navit_DEST_FILENAME;
    String dest_file_dir = CFG_FILENAME_PATH + "../export/";

    try {/*from w w w.j a v a 2 s .  co m*/
        File dir = new File(dest_file_dir);
        dir.mkdirs();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        File source = new File(orig_file);
        File destination = new File(dest_file_dir + Navit_DEST_FILENAME);

        if (source.exists()) {
            FileInputStream fi = new FileInputStream(source);
            FileOutputStream fo = new FileOutputStream(destination);
            FileChannel src = fi.getChannel();
            FileChannel dst = fo.getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            fi.close();
            fo.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.clark.func.Functions.java

/**
 * Internal copy file method.//from   w ww  .  j  a  v a 2 s.  c o  m
 * 
 * @param srcFile
 *            the validated source file, must not be <code>null</code>
 * @param destFile
 *            the validated destination file, must not be <code>null</code>
 * @param preserveFileDate
 *            whether to preserve the file date
 * @throws IOException
 *             if an error occurs
 */
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel input = null;
    FileChannel output = null;
    try {
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);
        input = fis.getChannel();
        output = fos.getChannel();
        long size = input.size();
        long pos = 0;
        long count = 0;
        while (pos < size) {
            count = (size - pos) > FIFTY_MB ? FIFTY_MB : (size - pos);
            pos += output.transferFrom(input, pos, count);
        }
    } finally {
        closeQuietly(output);
        closeQuietly(fos);
        closeQuietly(input);
        closeQuietly(fis);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }
}