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 main(String[] argv) throws Exception {
    FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();
    FileChannel dstChannel = new FileOutputStream("dstFilename").getChannel();

    dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
    srcChannel.close();/*ww w .  j av a2 s .  co  m*/
    dstChannel.close();
}

From source file:Copy.java

public static void main(String[] args) {
    FileChannel in = null;/*from w  w  w  .  j ava2  s . com*/
    FileChannel out = null;

    if (args.length < 2) {
        System.out.println("Usage: java Copy <from> <to>");
        System.exit(1);
    }

    try {
        in = new FileInputStream(args[0]).getChannel();
        out = new FileOutputStream(args[1]).getChannel();
        out.transferFrom(in, 0L, (int) in.size());

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean importDatabase(String dbName) {

    try {//from  www  . j ava2s . c o  m
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + PACKAGENAME + "//databases//" + dbName;
            String backupDBPath = "/AttendanceManager/" + dbName;
            File backupDB = new File(data, currentDBPath);
            File currentDB = new File(sd, backupDBPath);

            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) {
        return false;
    }
    return true;
}

From source file:Main.java

public static void saveDBInSdcard(Context pContext, String pDatabaseName) {
    try {/* w w  w.  ja  v a 2  s  .c  om*/
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + pContext.getPackageName() + "//databases//" + pDatabaseName;
            String backupDBPath = pDatabaseName;
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(pContext, backupDB.toString(), Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(pContext, e.toString(), Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

/**
 * Create a backup of the database.// w  w w .ja va2s . c om
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void exportDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank nach Downloads exportiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Exportieren fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

/**
 * Create a backup of the database.//from w w w.ja v a 2s  . c  o m
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void importDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File backupDB = new File(data, currentDBPath);
            File currentDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank von Downloads importiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Import fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

public static void restoreDb(File currentDB, String restoreDBFileName) {
    try {//from  www .j a v a 2 s .co  m
        backupDb(currentDB);
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File f = new File(sd.getAbsolutePath() + "/AccountManagement");
            if (!f.exists()) {
                f.mkdir();
            }
            String restoreDBPath = "AccountManagement/" + restoreDBFileName;
            File restoreDB = new File(sd, restoreDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(restoreDB).getChannel();
                FileChannel dst = new FileOutputStream(currentDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void backupDb(File currentDB) {
    try {// w w w.  j  a  v  a 2s .  c o m

        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File f = new File(sd.getAbsolutePath() + "/AccountManagement");
            if (!f.exists()) {
                f.mkdir();
            }
            Date currentDate = new Date();
            String currentDateStr = newFormat1.format(currentDate);

            currentDateStr = currentDateStr.trim();
            System.out.println(
                    "---------------------------------------------------------------" + currentDateStr);
            String backupDBPath = "AccountManagement/backup_" + currentDateStr + ".db";
            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) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void copyFileUsingFileChannels(File source, File dest) throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {//from  w  w w  .ja  va 2 s.c  om
        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

private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {/*  w  ww  .  j av  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();
    }
}