Java FileInputStream Copy copyFile(File source, File target)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File source, File target) throws IOException 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static void copyFile(File source, File target) throws IOException {
        // Need to recopy the file in one of these cases:
        // 1. target doesn't exists (never copied)
        // 2. if the target exists, compare their sizes, once defferent, for the copy.
        // 2. target exists but source has been modified recently(not used right now)

        if (!target.exists() || source.length() != target.length()
                || source.lastModified() > target.lastModified()) {
            copyFile(new FileInputStream(source), target);
        }//from  w w w .  j av  a 2s .  c o  m
    }

    public static void copyFile(InputStream source, File target) throws IOException {
        FileOutputStream fos = null;
        try {
            if (!target.getParentFile().exists()) {
                target.getParentFile().mkdirs();
            }

            fos = new FileOutputStream(target);
            byte[] buf = new byte[1024];
            int i = 0;
            while ((i = source.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
        } finally {
            try {
                source.close();
            } catch (Exception e) {
            }
            try {
                fos.close();
            } catch (Exception e) {
            }
        }

    }
}

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)
  5. copyFile(File source, File target)
  6. copyFile(File source, File target)
  7. copyFile(File source, File target)
  8. copyFile(File source, File target)
  9. copyFile(File source, File target, boolean createParents, FileFilter filter)