Example usage for java.nio.channels FileChannel transferFrom

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

Introduction

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

Prototype

public abstract long transferFrom(ReadableByteChannel src, long position, long count) throws IOException;

Source Link

Document

Transfers bytes into this channel's file from the given readable byte channel.

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());
        }/*from   w  ww . java 2s. co  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:org.neo4j.server.webadmin.AbstractWebadminTest.java

private static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();/*from   w  ww. j a  v  a  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 {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:Main.java

private static void CopyFile(String srcPath, String dstPath) {
    FileChannel srcChannel = null;
    FileChannel dstChannel = null;
    try {/*from w w  w.  j  a v a 2  s .c om*/
        // Open files
        srcChannel = (new FileInputStream(srcPath)).getChannel();
        dstChannel = (new FileOutputStream(dstPath)).getChannel();
        // Transfer the data
        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        // Close the files
        if (srcChannel != null) {
            srcChannel.close();
        }
        if (dstChannel != null) {
            dstChannel.close();
        }
    } catch (FileNotFoundException ex) {
        Log.i("CopyFile", "File not found " + ex.getMessage());
        ex.printStackTrace();
    } catch (IOException ex) {
        Log.i("CopyFile", "Transfer data error " + ex.getMessage());
        ex.printStackTrace();
    }
}

From source file:org.wso2.carbon.esb.vfs.transport.test.ESBJAVA3470.java

/**
 * Copy the given source file to the given destination
 *
 * @param sourceFile/*from  ww w  .j  a  v  a  2 s .  c om*/
 *                 source file
 * @param destFile
 *                 destination file
 * @throws IOException
 */
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;

    try {
        fileInputStream = new FileInputStream(sourceFile);
        fileOutputStream = new FileOutputStream(destFile);

        FileChannel source = fileInputStream.getChannel();
        FileChannel destination = fileOutputStream.getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(fileOutputStream);
    }
}

From source file:org.totschnig.myexpenses.Utils.java

static boolean copy(File src, File dst) {
    FileChannel srcC;/*from ww  w .j a v a 2  s .  co m*/
    try {
        srcC = new FileInputStream(src).getChannel();
        FileChannel dstC = new FileOutputStream(dst).getChannel();
        dstC.transferFrom(srcC, 0, srcC.size());
        srcC.close();
        dstC.close();
        return true;
    } catch (FileNotFoundException e) {
        Log.e("MyExpenses", e.getLocalizedMessage());
    } catch (IOException e) {
        Log.e("MyExpenses", e.getLocalizedMessage());
    }
    return false;
}

From source file:com.twentyoneechoes.borges.util.Utils.java

public static void backupDatabase(Context ctx) {
    try {/*  w  w w  . ja  v a 2  s  .c  o  m*/
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + ctx.getApplicationContext().getPackageName()
                    + "//databases//borges.db";
            String backupDBPath = "borges.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        Log.i(Utils.class.getSimpleName(), "Unable to backup database");
    }
}

From source file:com.jerrellmardis.amphitheatre.util.Utils.java

public static void backupDatabase(Context ctx) {
    try {// w w w.  j  a v  a2 s . c o  m
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + ctx.getApplicationContext().getPackageName()
                    + "//databases//amphitheatre.db";
            String backupDBPath = "amphitheatre.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        Log.i(Utils.class.getSimpleName(), "Unable to backup database");
    }
}

From source file:FileHelper.java

private static void copyFile(File srcFile, File destFile, long chunkSize) throws IOException {
    FileInputStream is = null;/*ww w. j ava  2  s . co m*/
    FileOutputStream os = null;
    try {
        is = new FileInputStream(srcFile);
        FileChannel iChannel = is.getChannel();
        os = new FileOutputStream(destFile, false);
        FileChannel oChannel = os.getChannel();
        long doneBytes = 0L;
        long todoBytes = srcFile.length();
        while (todoBytes != 0L) {
            long iterationBytes = Math.min(todoBytes, chunkSize);
            long transferredLength = oChannel.transferFrom(iChannel, doneBytes, iterationBytes);
            if (iterationBytes != transferredLength) {
                throw new IOException("Error during file transfer: expected " + iterationBytes + " bytes, only "
                        + transferredLength + " bytes copied.");
            }
            doneBytes += transferredLength;
            todoBytes -= transferredLength;
        }
    } finally {
        if (is != null) {
            is.close();
        }
        if (os != null) {
            os.close();
        }
    }
    boolean successTimestampOp = destFile.setLastModified(srcFile.lastModified());
    if (!successTimestampOp) {
        System.out.println("Could not change timestamp for {}. Index synchronization may be slow. " + destFile);
    }
}

From source file:com.germinus.easyconf.FileUtil.java

public static void copyFile(File source, File destination) {
    if (!source.exists()) {
        return;/*from   w w  w .  j  a  va  2 s .  c o  m*/
    }

    if ((destination.getParentFile() != null) && (!destination.getParentFile().exists())) {

        destination.getParentFile().mkdirs();
    }

    try {
        FileChannel srcChannel = new FileInputStream(source).getChannel();
        FileChannel dstChannel = new FileOutputStream(destination).getChannel();

        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

        srcChannel.close();
        dstChannel.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:org.wso2.bam.integration.tests.cassandra.KeySpaceNameChangeTestCase.java

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