Java Utililty Methods FileChannel Copy

List of utility methods to do FileChannel Copy

Description

The list of methods to do FileChannel Copy are organized into topic(s).

Method

longcopyStream(InputStream in, long length, File out)
copies the contens of a stream to a file
FileOutputStream stream = new FileOutputStream(out);
ReadableByteChannel cin = Channels.newChannel(in);
long result = copyChannel(cin, length, stream.getChannel());
stream.close();
return result;
voidcopyStreamToFile(InputStream inStream, File file)
copy Stream To File
try (ReadableByteChannel in = Channels.newChannel(inStream)) {
    try (FileOutputStream outStream = new FileOutputStream(file)) {
        FileChannel out = outStream.getChannel();
        long offset = 0;
        long quantum = 1024 * 1024;
        long count;
        while ((count = out.transferFrom(in, offset, quantum)) > 0) {
            offset += count;
...
voidcopyThrowsOnException(final File source, final File destination)
Copy src file to dst file.
try (FileInputStream inStream = new FileInputStream(source);
        FileOutputStream outStream = new FileOutputStream(destination)) {
    final FileChannel inChannel = inStream.getChannel();
    final FileChannel outChannel = outStream.getChannel();
    final long size = inChannel.size();
    long position = 0;
    while (position < size) {
        position += inChannel.transferTo(position, 1024L * 1024L, outChannel);
...
voidcopyTo(File srcFile, File destFile)
copy To
if (srcFile == null)
    throw new IllegalArgumentException("srcfile");
if (destFile == null)
    throw new IllegalArgumentException("destfile");
if (destFile.isDirectory()) {
    destFile = new File(destFile, srcFile.getName());
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
...
FilecopyToFolderAs(File fromFile, File folder, String asName)
copy To Folder As
mylogger.log(Level.INFO, "Copying:[{0}] file to folder [{1}]  as {2}",
        new Object[] { fromFile.getAbsolutePath(), folder.getAbsolutePath(), asName });
if (!folder.isDirectory() || !folder.exists()) {
    mylogger.log(Level.SEVERE, "Invalid folder {0} ! COPY FAILED",
            new Object[] { folder.getAbsolutePath() });
    return null;
if (!fromFile.exists()) {
...
FilecopyToTemp(File srcFile)
copy To Temp
File tmpFile = File.createTempFile("filecompare", "tmp");
if (srcFile.isDirectory()) {
    tmpFile.delete();
    tmpFile.mkdir();
copy(srcFile, tmpFile);
return tmpFile;
StringcopyToTempDirectory(final String file)
copy To Temp Directory
try {
    final String ending = file.substring(file.lastIndexOf('.'));
    final File tempfile = File.createTempFile("model", ending);
    copyFile(new File(file), tempfile);
    return tempfile.getAbsolutePath();
} catch (final IOException e) {
    e.printStackTrace();
return null;
voidcopyTransfer(String srcPath, String destPath)
copy Transfer
FileChannel srcChannel = new FileInputStream(srcPath).getChannel();
FileChannel destChannel = new FileOutputStream(destPath).getChannel();
try {
    srcChannel.transferTo(0, srcChannel.size(), destChannel);
} finally {
    srcChannel.close();
    destChannel.close();
voidcopyTree(File sourceLocation, File targetLocation)
copy Tree
if (sourceLocation.isDirectory()) {
    if (!targetLocation.exists()) {
        targetLocation.mkdirs();
        targetLocation.setLastModified(sourceLocation.lastModified());
    String[] children = sourceLocation.list();
    for (int i = 0; i < children.length; i++) {
        copyTree(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
...
voidcopyTree(File srcFile, File trgFile)
Copies a whole directory tree to another directory.
if (srcFile.isDirectory()) {
    trgFile.mkdir();
    for (File file : srcFile.listFiles()) {
        copyTree(file, new File(trgFile, file.getName()));
} else {
    FileChannel in = null, out = null;
    try {
...