Java FileInputStream Copy copyFile(File targetFile, File file)

Here you can find the source of copyFile(File targetFile, File file)

Description

copy File

License

Apache License

Declaration

public static void copyFile(File targetFile, File file) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

import java.io.IOException;

import java.io.FileNotFoundException;

public class Main {
    public static void copyFile(File targetFile, File file) {
        if (targetFile.exists()) {
            return;
        } else {//w w  w. ja  v  a 2 s .  c o  m
            createFile(targetFile, true);
        }
        try {
            FileInputStream is = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(targetFile);
            byte[] buffer = new byte[1024 * 5];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            is.close();
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void createFile(String path, boolean isFile) {
        createFile(new File(path), isFile);
    }

    public static void createFile(File file, boolean isFile) {
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                createFile(file.getParentFile(), false);
            } else {
                if (isFile) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    file.mkdir();
                }
            }
        }
    }
}

Related

  1. copyFile(File srcFile, File dstFile)
  2. copyFile(File srcPath, File dstPath)
  3. copyfile(File srFile, File dtFile)
  4. copyFile(File target, File source)
  5. copyFile(File targetFile, File file)
  6. copyFile(final File from, final File to)
  7. copyFile(final File fromFile, final File toFile)
  8. copyFile(final File fromFile, final File toFile)
  9. copyFile(final File in, final File out)