Java FileInputStream Copy copyFile(File source, File target, boolean replaceIfExists)

Here you can find the source of copyFile(File source, File target, boolean replaceIfExists)

Description

copy File

License

Open Source License

Declaration

private static void copyFile(File source, File target, boolean replaceIfExists) 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 {
    private static void copyFile(File source, File target, boolean replaceIfExists) throws IOException {
        if (!target.createNewFile()) {
            if (target.exists() && !replaceIfExists) {
                throw new IOException(String.format("File '%s' already exists. ", target.getAbsolutePath()));
            }//w  ww  .j av a2 s. co m
        }
        FileInputStream in = null;
        FileOutputStream out = null;
        byte[] b = new byte[8192];
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(target);
            int r;
            while ((r = in.read(b)) != -1) {
                out.write(b, 0, r);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

Related

  1. copyFile(File source, File target)
  2. copyFile(File source, File target)
  3. copyFile(File source, File target)
  4. copyFile(File source, File target, boolean createParents, FileFilter filter)
  5. copyFile(File source, File target, boolean deleteSourceAfter)
  6. copyFile(File sourceDir, File destDir, String filename)
  7. copyFile(File sourceFile, File destDir)
  8. copyFile(File sourceFile, File destFile)
  9. copyFile(File sourceFile, File destFile)