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:Main.java

public static void backupDb(File currentDB) {
    try {//from w w w .ja 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:FileUtils.java

public static void copyFile(File in, File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {//from ww w  .  ja v  a2 s .  c o m
        // magic number for Windows, 64Mb - 32Kb)
        int maxCount = (64 * 1024 * 1024) - (32 * 1024);
        long size = inChannel.size();
        long position = 0;
        while (position < size) {
            position += inChannel.transferTo(position, maxCount, outChannel);
        }
    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

From source file:Main.java

public static void copyFileFast(File in, File out) {
    FileChannel filein = null;
    FileChannel fileout = null;//from  ww  w.  j a v  a  2  s .c o  m
    try {
        filein = new FileInputStream(in).getChannel();
        fileout = new FileOutputStream(out).getChannel();
        filein.transferTo(0, filein.size(), fileout);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(filein, fileout);
    }
}

From source file:Main.java

public static void copyFile(@NonNull String pathFrom, @NonNull String pathTo) throws IOException {
    FileChannel outputChannel = null;
    FileChannel inputChannel = null;
    try {/*w  ww .j a  va2 s  . co  m*/
        inputChannel = new FileInputStream(new File(pathFrom)).getChannel();
        outputChannel = new FileOutputStream(new File(pathTo)).getChannel();
        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
        inputChannel.close();
    } finally {
        if (inputChannel != null)
            inputChannel.close();
        if (outputChannel != null)
            outputChannel.close();
    }

}

From source file:edu.usf.cutr.siri.SiriParserJacksonExample.java

/**
 * Read in input file to string/*  ww w . j  av  a  2 s. c  o m*/
 * @param file file containing the JSON or XML data
 * @return String representation of the JSON or XML file
 * @throws IOException
 */
private 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());
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:Grep.java

public static void setFile(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    FileChannel fc = fis.getChannel();

    // Get the file's size and then map it into memory
    int sz = (int) fc.size();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
    // Decode the file into a char buffer
    indexFile = decoder.decode(bb);/*from   w w w  .j  a  va 2  s  . co  m*/

    fc.close();
}

From source file:Main.java

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

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

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();//w ww.j a  v  a 2s .co  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:asia.gkc.vneedu.utils.FileUtil.java

/**
 * //from  w w w.  j  a v a  2 s  .  c o m
 *
 * @param source - ?
 * @param destination - ?
 * @return ??
 * @throws IOException
 */
public static boolean transferFile(File source, File destination) throws IOException {
    if (!destination.exists()) {
        logger.debug(destination.getPath());
        destination.createNewFile();
    }

    FileChannel src, dest;

    src = new FileInputStream(source).getChannel();
    dest = new FileOutputStream(destination).getChannel();

    long count = 0;
    long size = src.size();

    while ((count += dest.transferFrom(src, count, size - count)) < size)
        ;

    return true;
}