Example usage for java.nio.channels FileChannel close

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

Introduction

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

Prototype

public final void close() throws IOException 

Source Link

Document

Closes this channel.

Usage

From source file:Main.java

private static byte[] readFile2Bytes(final File file) {
    FileChannel fc = null;
    try {/*  w  ww.j  a v  a2 s .  c om*/
        fc = new RandomAccessFile(file, "r").getChannel();
        int size = (int) fc.size();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
        byte[] data = new byte[size];
        mbb.get(data, 0, size);
        return data;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (fc != null) {
                fc.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:edu.unc.lib.dl.util.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 {/*  www .jav a 2 s  .c o  m*/
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }

}

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

public static void backupDatabase(Context ctx) {
    try {//from  w  w w. j  a  v a  2  s .co 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:com.nridge.core.base.std.FilUtl.java

/**
 * Copies an input file to the output file.  In an effort to promote
 * better performance, I/O channels are used for the operations.
 *
 * @param anInFile Represents the source file.
 * @param anOutFile Represents the destination file.
 * @throws IOException Related to stream building and read/write operations.
 *//*w  ww.j  a v a  2  s  .  c om*/
static public void copy(File anInFile, File anOutFile) throws IOException

{
    FileChannel srcChannel, dstChannel;

    srcChannel = new FileInputStream(anInFile).getChannel();
    dstChannel = new FileOutputStream(anOutFile).getChannel();

    srcChannel.transferTo(0, srcChannel.size(), dstChannel);
    srcChannel.close();
    dstChannel.close();
}

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 {//w w w  .j  a v a2  s. c  om
        // 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:ja.lingo.engine.util.EngineFiles.java

private static void appendFile(String fileName, FileOutputStream fos, boolean prependWithLength)
        throws IOException {
    FileInputStream fis = new FileInputStream(fileName);
    FileChannel fic = fis.getChannel();
    FileChannel foc = fos.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(COPY_BUFFER_SIZE);

    // put header: length (1 int = 4 bytes)
    if (prependWithLength) {
        buffer.putInt((int) new File(fileName).length());
    }//w  w w .ja  va 2  s  .com

    // put body
    do {
        buffer.flip();
        foc.write(buffer);
        buffer.clear();
    } while (fic.read(buffer) != -1);
    fic.close();
    // NOTE: do not close 'foc'

    Files.delete(fileName);
}

From source file:org.gnucash.android.export.ExporterTask.java

/**
 * Copies a file from <code>src</code> to <code>dst</code>
 * @param src Absolute path to the source file
 * @param dst Absolute path to the destination file
 * @throws IOException if the file could not be copied
 *///from   ww w.j a  v  a 2  s  .  c om
public static void copyFile(File src, File dst) throws IOException {
    //TODO: Make this asynchronous at some time, t in the future.
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

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

public static void copyFile(File source, File destination) {
    if (!source.exists()) {
        return;//from w ww.j a v  a  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.ow2.proactive.resourcemanager.updater.RMNodeUpdater.java

private static boolean makeNodeUpToDate() {
    if (System.getProperty(NODE_URL_PROPERTY) != null) {

        String jarUrl = System.getProperty(NODE_URL_PROPERTY);
        String jarFile = SCHEDULER_NODE_JAR;

        if (System.getProperty(NODE_JAR_PROPERTY) != null) {
            jarFile = System.getProperty(NODE_JAR_PROPERTY);
        }/* w  ww  .  java2  s. c o m*/

        if (!isLocalJarUpToDate(jarUrl, jarFile)) {
            System.out.println("Downloading node.jar from " + jarUrl + " to " + jarFile);

            try {
                File destination = new File(jarFile);
                File lockFile = null;
                FileLock lock = null;

                if (destination.exists()) {

                    lockFile = new File(System.getProperty(TMP_DIR_PROPERTY) + "/lock");
                    if (!lockFile.exists()) {
                        lockFile.createNewFile();
                    }

                    System.out.println("Getting the lock on " + lockFile.getAbsoluteFile());
                    FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel();
                    lock = channel.lock();

                    if (isLocalJarUpToDate(jarUrl, jarFile)) {
                        System.out.println("Another process downloaded node.jar - don't do it anymore");
                        System.out.println("Releasing the lock on " + lockFile.getAbsoluteFile());
                        lock.release();
                        channel.close();

                        return false;
                    }
                }

                FileUtils.copyURLToFile(new URL(jarUrl), destination);
                System.out.println("Download finished");

                if (lock != null && lockFile != null) {
                    System.out.println("Releasing the lock on " + lockFile.getAbsoluteFile());
                    lock.release();
                }

                return true;

            } catch (Exception e) {
                System.err.println("Cannot download node.jar from " + jarUrl);
                e.printStackTrace();
            }
        }
    } else {
        System.out.println(
                "No java property " + NODE_URL_PROPERTY + " specified. Do not check for the new version.");
    }

    return false;
}

From source file:Main.java

public static File copyFileTo(File src, File dst) {
    FileChannel in = null;
    FileChannel out = null;/*from w  ww .ja v a2 s.  c  om*/
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    try {
        inStream = new FileInputStream(src);
        outStream = new FileOutputStream(dst);
        in = inStream.getChannel();
        out = outStream.getChannel();
        in.transferTo(0, in.size(), out);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            if (inStream != null) {
                inStream.close();
            }
            if (outStream != null) {
                outStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return dst;
}