Here you can find the source of copyFile(File from, File to)
Parameter | Description |
---|---|
from | from |
to | target |
Parameter | Description |
---|---|
IOException | on error |
public static void copyFile(File from, File to) throws IOException
//package com.java2s; //License from project: LGPL import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.Closeable; 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 { /**/*from w ww . j av a 2s. co m*/ * Copy a file from one location to the next. * * @param from from * @param to target * @throws IOException on error */ public static void copyFile(File from, File to) throws IOException { InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(from)); out = new BufferedOutputStream(new FileOutputStream(to)); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { close(in); close(out); } } /** * Closes a {@link Closeable} object, eating {@link IOException} errors. * Will do nothing if the passed object is null. * * @param o {@link Closeable} object */ public static void close(Closeable o) { if (o == null) return; try { o.close(); } catch (IOException e) { } } }