在文件操作时,经常会遇到对文件内容进行复制的操作
/**
* Copy one file's contents to another file. The target will be overwritten
* if it exists. The source must exist.
*/
public static void copy(File source, File target) throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(target);
final byte [] buffer = new byte [1024 * 8];
int len;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} finally {
close(fis, fos);
}
}