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:org.codehaus.preon.Codecs.java

public static <T> T decode(Codec<T> codec, Builder builder, File file)
        throws FileNotFoundException, IOException, DecodingException {
    FileInputStream in = null;//from w  w w .  j  a  va  2s  .  com
    FileChannel channel = null;
    try {
        in = new FileInputStream(file);
        channel = in.getChannel();
        int fileSize = (int) channel.size();
        ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
        return decode(codec, buffer, builder);
    } finally {
        if (channel != null) {
            channel.close();
        }
    }
}

From source file:Main.java

public static File copyFile(File destinationFile, File sourceFile) throws IOException {
    FileInputStream inputStream = null;
    FileChannel inputChannel = null;
    FileOutputStream outputStream = null;
    FileChannel outputChannel = null;
    try {//from   w  w w . j  a v  a  2s .  c  o m
        inputStream = new FileInputStream(sourceFile);
        inputChannel = inputStream.getChannel();
        outputStream = new FileOutputStream(destinationFile);
        outputChannel = outputStream.getChannel();
        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
        return destinationFile;
    } catch (IOException exception) {
        throw exception;
    } finally {
        if (inputChannel != null) {
            inputChannel.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputChannel != null) {
            outputChannel.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

From source file:net.ftb.util.FTBFileUtils.java

public static void copyFile(File sourceFile, File destinationFile, boolean overwrite) throws IOException {
    if (sourceFile.exists()) {
        if (!destinationFile.exists()) {
            destinationFile.getParentFile().mkdirs();
            destinationFile.createNewFile();
        } else if (!overwrite) {
            return;
        }//www .  j av a2s.  c  o  m
        FileChannel sourceStream = null, destinationStream = null;
        try {
            sourceStream = new FileInputStream(sourceFile).getChannel();
            destinationStream = new FileOutputStream(destinationFile).getChannel();
            destinationStream.transferFrom(sourceStream, 0, sourceStream.size());
        } finally {
            if (sourceStream != null) {
                sourceStream.close();
            }
            if (destinationStream != null) {
                destinationStream.close();
            }
        }
    }
}

From source file:com.bb.extensions.plugin.unittests.internal.utilities.FileUtils.java

/**
 * Copy sourceFile to destFile//from  w w w  . j  av  a  2  s. co m
 * 
 * @param sourceFile
 *            The source file to copy from
 * @param destFile
 *            The destination file to copy to
 * @throws IOException
 */
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    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:och.util.FileUtil.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.getParentFile().mkdirs();
        destFile.createNewFile();/*w ww . j  ava 2 s . c  o  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:com.aliyun.oss.integrationtests.TestUtils.java

public static String genFixedLengthFile(long fixedLength) throws IOException {
    ensureDirExist(TestBase.UPLOAD_DIR);
    String filePath = TestBase.UPLOAD_DIR + System.currentTimeMillis();
    RandomAccessFile raf = new RandomAccessFile(filePath, "rw");
    FileChannel fc = raf.getChannel();

    MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, fixedLength);
    try {//  www .  j  av a  2 s.co  m
        for (int i = 0; i < fixedLength; i++) {
            mbb.put(pickupAlphabet());
        }
        return filePath;
    } finally {
        if (fc != null) {
            fc.close();
        }

        if (raf != null) {
            raf.close();
        }
    }
}

From source file:com.weihuoya.bboo._G.java

public static boolean copyFile(File src, File dst) {
    FileInputStream instream = null;
    FileOutputStream outstream = null;
    FileChannel inchannel = null;
    FileChannel outchannel = null;

    try {/*from   w ww .j ava  2 s. co  m*/
        // channel
        instream = new FileInputStream(src);
        outstream = new FileOutputStream(dst);
        inchannel = instream.getChannel();
        outchannel = outstream.getChannel();
        // transfer
        inchannel.transferTo(0, inchannel.size(), outchannel);
        // close
        inchannel.close();
        outchannel.close();
        instream.close();
        outstream.close();
        //
        return true;
    } catch (IOException e) {
        _G.log(e.toString());
    }
    return false;
}

From source file:org.fusesource.mop.support.Database.java

static private void copy(File from, File to) throws IOException {
    to.delete();/*w  ww  . ja va  2 s .c  om*/
    FileChannel in = new FileInputStream(from).getChannel();
    try {

        File tmp = File.createTempFile(to.getName(), ".part", to.getParentFile());
        FileChannel out = new FileOutputStream(tmp).getChannel();
        try {
            out.transferFrom(in, 0, from.length());
        } finally {
            out.close();
        }

        tmp.renameTo(to);
    } finally {
        in.close();
    }
}

From source file:it.isislab.dmason.util.SystemManagement.Worker.Updater.java

public static void copyFile(File sfile, File dfile) throws Exception {
    FileChannel source = new FileInputStream(sfile).getChannel();
    FileChannel dest = new FileOutputStream(dfile).getChannel();
    source.transferTo(0, source.size(), dest);
    source.close();
    dest.close();//from w  w w.  j  ava 2 s  .  c  om

}

From source file:com.bjorsond.android.timeline.utilities.Utilities.java

public static void copyFile(String fromFile, String toPath, String toFilename) {

    System.out.println("COPY!");
    if (Environment.getExternalStorageState().equals("mounted")) {
        File sdCardDirectory = Environment.getExternalStorageDirectory();

        try {//  www.  java2  s . c o  m
            if (sdCardDirectory.canWrite()) {

                File destinationDirectory = new File(toPath);
                File sourceFile = new File(fromFile);
                File destinationFile = new File(destinationDirectory, toFilename);

                if (!destinationDirectory.exists()) {
                    destinationDirectory.mkdirs();
                }

                FileChannel source = new FileInputStream(sourceFile).getChannel();
                FileChannel destination = new FileOutputStream(destinationFile).getChannel();
                destination.transferFrom(source, 0, source.size());
                source.close();
                destination.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}