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:edu.kit.cockpit.valuationserver.sfmpersistency.FileUtil.java

/**
 * @param file/*  w w  w  .j  a  v  a2s .  co m*/
 * @return string representation of File
 * @throws IOException
 */
public static String readFile(File file) throws IOException {
    FileInputStream stream = new FileInputStream(file);
    try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:Main.java

public static void copyFdToFile(FileDescriptor src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {// w  w  w  . j ava  2s  .c om
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:Main.java

private static void copyFile(File src, File dst) throws IOException {
    FileInputStream in = new FileInputStream(src);
    FileOutputStream out = new FileOutputStream(dst);
    FileChannel fromChannel = null, toChannel = null;
    try {/*ww  w .ja  v  a2 s  .c  om*/
        fromChannel = in.getChannel();
        toChannel = out.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        if (fromChannel != null)
            fromChannel.close();
        if (toChannel != null)
            toChannel.close();
    }
}

From source file:fm.moe.android.util.JSONFileHelper.java

private static String readFile(final File file) throws IOException {
    final FileInputStream stream = new FileInputStream(file);
    try {/*from ww  w  .jav a2 s. c o  m*/
        final FileChannel fc = stream.getChannel();
        final MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:Main.java

private static void copyFile(File src, File dst) throws Exception {
    FileChannel inChannel = (new FileInputStream(src)).getChannel();
    FileChannel outChannel = (new FileOutputStream(dst)).getChannel();
    try {//from   ww  w.j a v a  2 s  . c  om
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

From source file:Main.java

public static void copyFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {/*from   ww  w.jav  a2 s .c  om*/
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:Main.java

public static void copyFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {//from w w w  .j  av a  2s  . co  m
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
            outChannel.close();
        }
    }
}

From source file:Main.java

public static boolean importDatabase(String dbName) {

    try {//from   www. j av  a2s.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 {/*from  ww  w  .  j  a va 2s .  com*/
        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

/** Copia un fichero.
 * @param source Fichero origen con el contenido que queremos copiar.
 * @param dest Fichero destino de los datos.
 * @throws IOException SI ocurre algun problema durante la copia */
public static void copyFile(final File source, final File dest) throws IOException {
    if (source == null || dest == null) {
        throw new IllegalArgumentException("Ni origen ni destino de la copia pueden ser nulos"); //$NON-NLS-1$
    }/*from  w  ww  .  j  av  a 2s . c om*/

    final FileInputStream is = new FileInputStream(source);
    final FileOutputStream os = new FileOutputStream(dest);
    final FileChannel in = is.getChannel();
    final FileChannel out = os.getChannel();
    final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
    out.write(buf);

    in.close();
    out.close();
    is.close();
    os.close();

}