Here you can find the source of copyFile(final File to, final File from)
Parameter | Description |
---|---|
to | The file to be copied to (must already exist). |
from | The file to be copied from. |
public static void copyFile(final File to, final File from) throws IOException
//package com.java2s; /*/* www. j a v a2 s .c o m*/ * Copyright (C) 2015 Red Hat, Inc. and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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 { private static final int BUF_SIZE = 1024; /** * Copy the contents of a file. * * @param to * The file to be copied to (must already exist). * @param from * The file to be copied from. */ public static void copyFile(final File to, final File from) throws IOException { BufferedInputStream reader = new BufferedInputStream(new FileInputStream(from)); BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(to)); copyStream(writer, reader); writer.close(); reader.close(); } /** * Copy one stream to another until the input stream has no more data. * * @param to * The open stream to write to. * @param from * The open stream to read from. */ public static void copyStream(final OutputStream to, final InputStream from) throws IOException { final byte[] buf = new byte[BUF_SIZE]; int len = from.read(buf); while (len > 0) { to.write(buf, 0, len); len = from.read(buf); } } }