Description
Copies a file.
License
Apache License
Parameter
Parameter | Description |
---|
target | the target file |
source | the source file |
Exception
Parameter | Description |
---|
IOException | in any case of I/O problem, trying to close the involved files before end if possible |
Declaration
public static void copyFile(File target, File source) throws IOException
Method Source Code
//package com.java2s;
/*// ww w . j a v a 2 s . c o m
* Copyright 2009-2017 University of Hildesheim, Software Systems Engineering
*
* 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.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Main {
/**
* Copies a file.
*
* @param target the target file
* @param source the source file
* @throws IOException in any case of I/O problem, trying to close the involved files before end if possible
*/
public static void copyFile(File target, File source) throws IOException {
FileInputStream in = null;
try {
in = new FileInputStream(source);
copyToFile(target, in);
in.close();
} catch (IOException e) {
if (null != in) {
closeQuietly(in);
}
throw e;
}
}
/**
* Copies the contents of <code>in</code> at the current position to <code>file</code>.
* Does not attempt to close <code>in</code>. Uses a default buffer of size 1024.
*
* @param file the file to write to
* @param in the input stream
* @throws IOException in any case of I/O problem, trying to close <code>file</code> before end if possible
*/
public static void copyToFile(File file, InputStream in) throws IOException {
copyToFile(file, in, null);
}
/**
* Copies the contents of <code>in</code> at the current position to <code>file</code>.
* Does not attempt to close <code>in</code>. Uses a default buffer of size 1024.
*
* @param file the file to write to
* @param in the input stream
* @param buffer a copy buffer (if <b>null</b> a default one of size 1024 is used)
* @throws IOException in any case of I/O problem, trying to close <code>file</code> before if possible
*/
public static void copyToFile(File file, InputStream in, byte[] buffer) throws IOException {
if (null == buffer) {
buffer = new byte[1024];
}
FileOutputStream fos = new FileOutputStream(file);
try {
int read;
do {
read = in.read(buffer);
if (read > 0) {
fos.write(buffer, 0, read);
}
} while (read > 0);
} catch (IOException e) {
closeQuietly(fos);
throw e;
}
fos.close();
}
/**
* Closes a closeable quietly, i.e., without exception.
*
* @param closeable the closeable
*/
public static void closeQuietly(Closeable closeable) {
if (null != closeable) {
try {
closeable.close();
} catch (IOException e1) {
}
}
}
}
Related
- copyFile(File srcFile, File dstFile)
- copyFile(File srcFile, File dstFile)
- copyFile(File srcFile, File dstFile)
- copyFile(File srcPath, File dstPath)
- copyfile(File srFile, File dtFile)
- copyFile(File targetFile, File file)
- copyFile(File targetFile, File file)
- copyFile(final File from, final File to)
- copyFile(final File fromFile, final File toFile)