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:nz.govt.natlib.ndha.common.FileUtils.java

/**
 * Name is self explanatory/*from  w  w  w .java  2  s  .c  o  m*/
 *
 * @param from
 * @param to
 * @throws IOException
 */
public static void copyFile(File from, File to) throws IOException {
    FileChannel inChannel = new FileInputStream(from).getChannel();
    FileChannel outChannel = new FileOutputStream(to).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:Main.java

public static void copyFiles(String sourcePath, String targetPath) throws IOException {
    File srcDir = new File(sourcePath);
    File[] files = srcDir.listFiles();
    FileChannel in = null;
    FileChannel out = null;/*w ww .  java  2  s. co  m*/
    for (File file : files) {
        try {
            in = new FileInputStream(file).getChannel();
            File outDir = new File(targetPath);
            if (!outDir.exists()) {
                outDir.mkdirs();
            }
            File outFile = new File(targetPath + File.separator + file.getName());
            out = new FileOutputStream(outFile).getChannel();
            in.transferTo(0, in.size(), out);
        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }
}

From source file:org.darkware.wpman.wpcli.WPCLI.java

/**
 * Update the local WP-CLI tool to the most recent version.
 *//*from  www  .j a  v a2  s  .co m*/
public static void update() {
    try {
        WPManager.log.info("Downloading new version of WP-CLI.");

        CloseableHttpClient httpclient = HttpClients.createDefault();

        URI pharURI = new URIBuilder().setScheme("http").setHost("raw.githubusercontent.com")
                .setPath("/wp-cli/builds/gh-pages/phar/wp-cli.phar").build();

        WPManager.log.info("Downloading from: {}", pharURI);
        HttpGet downloadRequest = new HttpGet(pharURI);

        CloseableHttpResponse response = httpclient.execute(downloadRequest);

        WPManager.log.info("Download response: {}", response.getStatusLine());
        WPManager.log.info("Download content type: {}", response.getFirstHeader("Content-Type").getValue());

        FileChannel wpcliFile = FileChannel.open(WPCLI.toolPath, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);

        response.getEntity().writeTo(Channels.newOutputStream(wpcliFile));
        wpcliFile.close();

        Set<PosixFilePermission> wpcliPerms = new HashSet<>();
        wpcliPerms.add(PosixFilePermission.OWNER_READ);
        wpcliPerms.add(PosixFilePermission.OWNER_WRITE);
        wpcliPerms.add(PosixFilePermission.OWNER_EXECUTE);
        wpcliPerms.add(PosixFilePermission.GROUP_READ);
        wpcliPerms.add(PosixFilePermission.GROUP_EXECUTE);

        Files.setPosixFilePermissions(WPCLI.toolPath, wpcliPerms);
    } catch (URISyntaxException e) {
        WPManager.log.error("Failure building URL for WPCLI download.", e);
        System.exit(1);
    } catch (IOException e) {
        WPManager.log.error("Error while downloading WPCLI client.", e);
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:com.edgenius.core.util.FileUtil.java

public static void copyFile(File in, File out) throws Exception {
    FileChannel sourceChannel = new FileInputStream(in).getChannel();
    FileChannel destinationChannel = new FileOutputStream(out).getChannel();
    sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
    sourceChannel.close();
    destinationChannel.close();//from   w w  w .  jav  a2s. c o m
}

From source file:org.easysoa.registry.frascati.FileUtils.java

/**
 * Copy a file in an other file/*from  w  ww  .j  a va 2 s . c o  m*/
 * 
 * @param source The source file
 * @param target The target file
 * @throws Exception If a problem occurs
 */
public static final void copyTo(File source, File target) throws Exception {
    if (source == null || target == null) {
        throw new IllegalArgumentException("Source and target files must not be null");
    }
    // Input and outputs channels
    log.debug("source file = " + source);
    log.debug("target file = " + target);
    FileChannel in = null;
    FileChannel out = null;
    try {
        // Init
        in = new FileInputStream(source).getChannel();
        out = new FileOutputStream(target).getChannel();
        // File copy
        in.transferTo(0, in.size(), out);
    } catch (Exception ex) {
        throw ex;
    } finally {
        // finally close all streams
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.messic.service.MessicMain.java

private static void copyFile(File source, File dest) throws IOException {
    FileChannel sourceChannel = null;
    FileChannel destChannel = null;
    try {//from   w ww .  jav  a 2s . c om
        sourceChannel = new FileInputStream(source).getChannel();
        destChannel = new FileOutputStream(dest).getChannel();
        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
    } finally {
        sourceChannel.close();
        destChannel.close();
    }
}

From source file:org.wso2.bam.integration.tests.cassandra.KeySpaceNameChangeTestCase.java

private static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();/*from  w ww  .  j a  va 2 s .co  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:com.becapps.easydownloader.utils.Utils.java

@SuppressWarnings("resource")
public static void copyFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    //if (!dst.exists()) {
    try {//w w w.j a  v  a2s  .  c o m
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
    /*} else {
       logger("w", "copyFile: destination already exists", DEBUG_TAG);
    }*/
}

From source file:org.nuclos.common2.IOUtils.java

/**
 * @param fileIn/*w  w  w.ja  v  a  2  s. c  om*/
 * @param fileOut
 * @throws IOException
 */
public static void copyFile(File fileIn, File fileOut) throws IOException {
    FileChannel sourceChannel = null;
    FileChannel destinationChannel = null;
    try {
        sourceChannel = new FileInputStream(fileIn).getChannel();
        destinationChannel = new FileOutputStream(fileOut).getChannel();
        sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
    } finally {
        try {
            if (sourceChannel != null) {
                sourceChannel.close();
            }
        } finally {
            if (destinationChannel != null) {
                destinationChannel.close();
            }
        }
    }
}

From source file:net.ftb.util.FileUtils.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;
        FileChannel sourceStream = null, destinationStream = null;
        try {//from  w w  w .j a  v  a2  s . co  m
            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();
            }
        }
    }
}