Java BufferedInputStream Copy copyFile(File sourceFile, File targetFile)

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

Description

copy File

License

Open Source License

Declaration

static public void copyFile(File sourceFile, File targetFile) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  ww  w  .j  av  a  2s.  c  o m*/
 * Copyright (c) 2010 Ben Fry and Casey Reas
 *
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.opensource.org/licenses/eclipse-1.0.php
 */

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 public void copyFile(File sourceFile, File targetFile) throws IOException {
        InputStream from = new BufferedInputStream(new FileInputStream(sourceFile));
        OutputStream to = new BufferedOutputStream(new FileOutputStream(targetFile));
        byte[] buffer = new byte[16 * 1024];
        int bytesRead;
        while ((bytesRead = from.read(buffer)) != -1) {
            to.write(buffer, 0, bytesRead);
        }
        to.flush();
        from.close(); // ??
        from = null;
        to.close(); // ??
        to = null;

        targetFile.setLastModified(sourceFile.lastModified());
    }
}

Related

  1. copyFile(File source, File destination)
  2. copyFile(File source, File destination, boolean overwrite)
  3. copyFile(File source, File target)
  4. copyFile(File source, File target)
  5. copyFile(File source, File target)
  6. copyFile(File sourceFile, File targetFile)
  7. copyFile(File sourceFile, File targetFile)
  8. copyFile(File src, File dest)
  9. copyFile(File src, File dest)