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:org.neo4j.server.webadmin.AbstractWebadminTest.java

private static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();/*ww  w.  j  ava 2s .  c om*/
    }

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

public static boolean copyFile(File fromFile, File toFile) {
    boolean ok = false;
    try {/*w w  w. j av a  2 s .  com*/
        // 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:org.uva.itast.blended.omr.pages.PDFPageImage.java

/**
 * @param filePath2//w ww.  j  av  a  2  s.  co m
 * @throws IOException 
 */
public static PDFFile loadPDFFile(File inputpath) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(inputpath, "r"); //se carga la imagen pdf para leerla
    FileChannel channel = raf.getChannel();
    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    PDFFile pdffile = new PDFFile(buf);
    return pdffile;
}

From source file:org.wso2.carbon.esb.vfs.transport.test.ESBJAVA4679VFSPasswordSecurityTestCase.java

/**
 * Copy the given source file to the given destination
 *
 * @param sourceFile source file/*from  w w  w .j  a v a 2 s  . co m*/
 * @param destFile   destination file
 * @throws java.io.IOException
 */
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileInputStream = new FileInputStream(sourceFile);
        fileOutputStream = new FileOutputStream(destFile);

        FileChannel source = fileInputStream.getChannel();
        FileChannel destination = fileOutputStream.getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(fileOutputStream);
    }
}

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 {//from w ww .j a va  2  s .  com
        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: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 ww .j a  v a2  s.c om*/
        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:org.ednovo.gooru.application.util.GooruImageUtil.java

public static PDFFile getPDFFile(String pdfPath) {
    ByteBuffer buf;//from   w ww  .j  a  v  a  2s  .  c  o  m
    PDFFile pdfFile = null;
    try {
        File file = new File(pdfPath);
        @SuppressWarnings("resource")
        RandomAccessFile accessFile = new RandomAccessFile(file, "r");
        FileChannel channel = accessFile.getChannel();
        buf = channel.map(MapMode.READ_ONLY, 0, channel.size());
        pdfFile = new PDFFile(buf);
    } catch (Exception e) {
        LOGGER.error("getPDFFile: " + e);
    }
    return pdfFile;

}

From source file:Main.java

private static void CopyFile(String srcPath, String dstPath) {
    FileChannel srcChannel = null;
    FileChannel dstChannel = null;
    try {//from w w  w  . j  a  v a 2  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:r2b.apps.utils.FileUtils.java

/**
 * Copy src to dst./*from  www  . j a v a2s . com*/
 * @param src Source file.
 * @param dst Destination file.
 */
public static void copy(final File src, File dst) {
    try {
        FileInputStream inStream = new FileInputStream(src);
        FileOutputStream outStream = new FileOutputStream(dst);
        FileChannel inChannel = inStream.getChannel();
        FileChannel outChannel = outStream.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        inStream.close();
        outStream.close();
    } catch (IOException e) {
        Logger.e(FileUtils.class.getSimpleName(), e.toString());
    }
}

From source file:Main.java

public static boolean fileCopy(File srcFile, File dstFile) {
    int length = 1048891;
    FileChannel inC = null;
    FileChannel outC = null;/* w  w w .j ava 2 s . c  o m*/
    try {
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(dstFile);
        inC = in.getChannel();
        outC = out.getChannel();
        ByteBuffer b = null;
        while (inC.position() < inC.size()) {
            if ((inC.size() - inC.position()) < length) {
                length = (int) (inC.size() - inC.position());
            } else {
                length = 1048891;
            }
            b = ByteBuffer.allocateDirect(length);
            inC.read(b);
            b.flip();
            outC.write(b);
            outC.force(false);
        }

        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (inC != null && inC.isOpen()) {
                inC.close();
            }
            if (outC != null && outC.isOpen()) {
                outC.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}