Java FileChannel Copy copyFile(String in, String out)

Here you can find the source of copyFile(String in, String out)

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(String in, String out) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    public static boolean copyFile(String in, String out) {
        File inFile = new File(in);
        File outFile = new File(out);
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {/*from www. j a v  a  2 s  .  c  o  m*/
            inChannel = new FileInputStream(inFile).getChannel();
            outChannel = new FileOutputStream(outFile).getChannel();

            // hack for files larger than 64Mb
            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) {
            return false;
        } finally {
            if (inChannel != null) {
                try {
                    inChannel.close();
                } catch (Exception e) {
                }
            }
            if (outChannel != null) {
                try {
                    outChannel.close();
                } catch (Exception e) {
                }
            }
        }
        return true;
    }
}

Related

  1. copyFile(java.io.File fromFile, java.io.File toFile)
  2. copyFile(java.io.File in, java.io.File out)
  3. copyFile(Path source, Path target)
  4. copyFile(String fromFileName, String toFileName)
  5. copyFile(String fromPath, String toPath)
  6. copyFile(String infile, String outfile)
  7. copyFile(String inFile, String outFile)
  8. copyFile(String inName, String otName)
  9. copyFile(String input, String output)