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:com.bellman.bible.service.common.FileManager.java

public static boolean copyFile(File fromFile, File toFile) {
    boolean ok = false;
    try {/*w  ww . java 2 s .  c  o  m*/
        // don't worry if tofile exists, allow overwrite
        if (fromFile.exists()) {
            //ensure the target dir exists or FileNotFoundException is thrown creating dst FileChannel
            File toDir = toFile.getParentFile();
            toDir.mkdir();

            long fromFileSize = fromFile.length();
            log.debug("Source file length:" + fromFileSize);
            if (fromFileSize > CommonUtils.getFreeSpace(toDir.getPath())) {
                // not enough room on SDcard
                ok = false;
            } else {
                // move the file
                FileInputStream srcStream = new FileInputStream(fromFile);
                FileChannel src = srcStream.getChannel();
                FileOutputStream dstStream = new FileOutputStream(toFile);
                FileChannel dst = dstStream.getChannel();
                try {
                    dst.transferFrom(src, 0, src.size());
                    ok = true;
                } finally {
                    src.close();
                    dst.close();
                    srcStream.close();
                    dstStream.close();
                }
            }
        } else {
            // fromfile does not exist
            ok = false;
        }
    } catch (Exception e) {
        log.error("Error moving file to sd card", e);
    }
    return ok;
}

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

public static void backupDatabase(Context ctx) {
    try {/*from   w ww . j  a  va  2s  . co 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: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());
        }/*www.  jav a 2s.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();
        }
    }
}

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

static boolean copy(File src, File dst) {
    FileChannel srcC;
    try {/*from   www.  ja v  a  2s  .  c  o  m*/
        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:org.alfresco.repo.content.http.HttpAlfrescoStore.java

private static void doTest(ApplicationContext ctx, String baseUrl, String contentUrl) throws Exception {
    ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    TransactionService transactionService = serviceRegistry.getTransactionService();
    AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
    // Construct the store
    HttpAlfrescoStore store = new HttpAlfrescoStore();
    store.setTransactionService(transactionService);
    store.setAuthenticationService(authenticationService);
    store.setBaseHttpUrl(baseUrl);/*from ww w  .  ja  va 2  s  .  c o  m*/

    // Now test
    System.out.println("   Retrieving reader for URL " + contentUrl);
    ContentReader reader = store.getReader(contentUrl);
    System.out.println("   Retrieved reader for URL " + contentUrl);
    // Check if the content exists
    boolean exists = reader.exists();
    if (!exists) {
        System.out.println("   Content doesn't exist: " + contentUrl);
        return;
    } else {
        System.out.println("   Content exists: " + contentUrl);
    }
    // Get the content data
    ContentData contentData = reader.getContentData();
    System.out.println("   Retrieved content data: " + contentData);

    // Now get the content
    ByteBuffer buffer = ByteBuffer.allocate((int) reader.getSize());
    FileChannel channel = reader.getFileChannel();
    try {
        int count = channel.read(buffer);
        if (count != reader.getSize()) {
            System.err.println("The number of bytes read was " + count + " but expected " + reader.getSize());
            return;
        }
    } finally {
        channel.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. jav a2  s  .c  o  m*/
        // 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.nuclos.common2.File.java

/**
 * Simple Method for copying files with FileChannel
 * FIX ELISA-6498/* www.  j a v  a 2  s. c om*/
 * 
 * @param in
 * @param out
 * @throws IOException
 */
public static void copyFile(java.io.File in, java.io.File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {
        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:de.knowwe.visualization.util.Utils.java

private static boolean isFileClosedWindows(File file) {
    boolean closed;
    FileChannel channel = null;
    try {/*from  www  . ja v  a  2s .c om*/
        channel = new RandomAccessFile(file, "rw").getChannel();
        closed = true;
    } catch (Exception ex) {
        closed = false;
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException ex) {
                // exception handling
            }
        }
    }
    return closed;
}

From source file:Main.java

@SuppressWarnings("unused")
private static boolean copy2SingleFileByChannel(File sourceFile, File destFile) {
    boolean copyOK = true;
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {//from w  w  w.  ja  va2  s .  c o  m
        inputStream = new FileInputStream(sourceFile);
        outputStream = new FileOutputStream(destFile);
        inputChannel = inputStream.getChannel();
        outputChannel = outputStream.getChannel();
        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
    } catch (Exception e) {
        copyOK = false;
    } finally {
        try {
            inputChannel.close();
            inputStream.close();
            outputChannel.close();
            outputStream.close();
        } catch (IOException e) {
            copyOK = false;
            e.printStackTrace();
        }
    }
    return copyOK;
}

From source file:ValidateLicenseHeaders.java

/**
 * Replace a legacy jboss header with the current default header
 * //from ww w .  j a  v a2  s  .co  m
 * @param javaFile -
 *          the java source file
 * @param endOfHeader -
 *          the offset to the end of the legacy header
 * @throws IOException -
 *           thrown on failure to replace the header
 */
static void replaceHeader(File javaFile, long endOfHeader) throws IOException {
    if (log.isLoggable(Level.FINE))
        log.fine("Replacing legacy jboss header in: " + javaFile);
    RandomAccessFile raf = new RandomAccessFile(javaFile, "rw");
    File bakFile = new File(javaFile.getAbsolutePath() + ".bak");
    FileOutputStream fos = new FileOutputStream(bakFile);
    fos.write(DEFAULT_HEADER.getBytes());
    FileChannel fc = raf.getChannel();
    long count = raf.length() - endOfHeader;
    fc.transferTo(endOfHeader, count, fos.getChannel());
    fc.close();
    fos.close();
    raf.close();
    if (javaFile.delete() == false)
        log.severe("Failed to delete java file: " + javaFile);
    if (bakFile.renameTo(javaFile) == false)
        throw new SyncFailedException("Failed to replace: " + javaFile);
}