您的位置:首页 > 其它

复制文件处理

2012-03-26 14:42 239 查看
/**

* 复制文件。targetFile为目标文件,file为源文件

*

* @param targetFile

* @param file

*/

public static void copyFile(File targetFile, File file) {

if (targetFile.exists()) {

System.out.println("文件" + targetFile.getAbsolutePath()

+ "已经存在,跳过该文件!");

return;

} else {

createFile(targetFile, true);

}

InputStream is = null;

FileOutputStream fos = null;

System.out.println("复制文件" + file.getName() + "到"

+ targetFile.getAbsolutePath());

try {

is = new FileInputStream(file);

fos = new FileOutputStream(targetFile);

byte[] buffer = new byte[1024];

int length = 0;

while ((length = is.read(buffer)) != -1) {

fos.write(buffer, 0, length);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

IOUtils.closeQuietly(is);

IOUtils.closeQuietly(fos);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: