Java FileInputStream Copy copyFile(final String sourceFilePath, final String destFilePath)

Here you can find the source of copyFile(final String sourceFilePath, final String destFilePath)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(final String sourceFilePath, final String destFilePath) throws IOException 

Method Source Code


//package com.java2s;

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

public class Main {
    public static void copyFile(final String sourceFilePath, final String destFilePath) throws IOException {
        final File sourceFile = new File(sourceFilePath);
        final File destFile = new File(destFilePath);

        if (!destFile.exists()) {
            destFile.createNewFile();/* w  ww .  j ava  2  s . c o m*/
        } else {
            destFile.delete();
            destFile.createNewFile();
        }

        FileInputStream source = null;
        FileOutputStream destination = null;
        try {
            source = new FileInputStream(sourceFile);
            destination = new FileOutputStream(destFile);

            // previous code: destination.transferFrom(source, 0,
            // source.size());
            // to avoid infinite loops, should be:
            long count = 0;
            final long size = source.getChannel().size();
            while ((count += destination.getChannel().transferFrom(source.getChannel(), count,
                    size - count)) < size) {
                ;
            }
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
}

Related

  1. copyFile(final File srcFile, final File destFile)
  2. copyFile(final File srcPath, final File dstPath)
  3. copyFile(final InputStream in, final File destFile)
  4. copyFile(final String from, final String to)
  5. copyFile(final String sourceFile, final String destinationFile)
  6. copyFile(final String sSource, final String sDest)
  7. copyFile(InputStream in, File dst)
  8. copyFile(InputStream in, File to)
  9. copyFile(InputStream in, String destFile)