Java BufferedInputStream Copy copyFile(final File from, final File to)

Here you can find the source of copyFile(final File from, final File to)

Description

copy File

License

LGPL

Declaration

public static void copyFile(final File from, final File to) throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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

import java.io.OutputStream;

public class Main {
    static final int BUFFER = 8 * 1024;

    public static void copyFile(final File from, final File to) throws IOException {
        final InputStream inputStream = new BufferedInputStream(new FileInputStream(from));
        copyFile(inputStream, to);// w w w  .j a  v a 2 s  . c  o m
    }

    public static void copyFile(final InputStream inputStream, final File to) throws IOException {
        createFile(to);
        OutputStream outputStream = null;
        try {
            copyStream(inputStream, outputStream = new BufferedOutputStream(new FileOutputStream(to)));
        } finally {
            inputStream.close();
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

    public static File createFile(final File file) throws IOException {
        if (!file.exists()) {
            createDirectoryRecursively(file.getParentFile());
            file.createNewFile();
        }
        return file;
    }

    public static int copyStream(final InputStream inputStream, final OutputStream outputStream)
            throws IOException {
        int result = 0;
        final byte[] buf = new byte[BUFFER];
        for (;;) {
            final int numRead = inputStream.read(buf);
            if (numRead == -1) {
                break;
            }
            outputStream.write(buf, 0, numRead);
            result += numRead;
        }
        outputStream.flush();
        return result;
    }

    public static boolean createDirectoryRecursively(File directory) {
        if (directory == null) {
            return false;
        } else if (directory.exists()) {
            return !directory.isFile();
        } else if (!directory.isAbsolute()) {
            directory = new File(directory.getAbsolutePath());
        }
        final String parent = directory.getParent();
        if ((parent == null) || !createDirectoryRecursively(new File(parent))) {
            return false;
        }
        directory.mkdir();
        return directory.exists();
    }
}

Related

  1. copyFile(File src, File targetDir, boolean onlyNew)
  2. copyFile(File src, String target)
  3. copyFile(File srcFile, File destFile, boolean createCopy)
  4. copyFile(File srcFile, File detFolder)
  5. copyFile(File srcFile, File targetFolder)
  6. copyFile(final File fSource, final File fDest)
  7. copyFile(final File source, final File dest, final boolean overwrite)
  8. copyFile(final File source, final File destination)
  9. copyFile(final File source, final File destination, final boolean overwrite)