复制文件处理
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);
}
}
* 复制文件。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);
}
}
相关文章推荐
- 批处理查找并复制文件到指定文件夹
- dd命令_Linux dd 命令用法详解:复制文件并对原文件的内容进行转换和格式化处理
- 批处理复制文件并重命名
- java对文件的处理(读取,写入,复制,加密和解密)
- ASP.NET 一般处理文件,复制以前文件,无法调试,无法访问的问题
- 处理大文本文件(GB级别),查找,替换,复制
- 【转】巧用DOS tree命令+批处理 实现 指定文件 批量复制!
- Ubuntu,两个服务器之间 复制文件方法(还包括一些基本的数据处理的部分)
- Java中复制文件和文件夹的操作,异常未处理,无注释-_-!
- windows批处理:复制文件到指定路径和删除指定路径内的文件
- VirtalBox之复制虚拟硬盘文件故障处理
- 批处理中复制文件:copy
- windows 2003 复制大文件提示系统资源不足的官方处理方法
- 批处理复制文件(文件夹)排除某目录,某些类型(草稿)
- No buffer space available 和windows 2003复制文件时:配额不足,无法处理该命令
- 处理大文本文件(GB级别),查找,替换,复制
- 发布网站时报错:未能将文件xxx复制到xxx,问题处理
- java web中监听本地磁盘文件,并进行上传,复制,处理等
- Java IO 之 FileReader FileWriter 实现字符文件的复制(处理FileWriter新建文件失败的问题)
- 安装IIS提示安装程序无法复制文件staxmem.dll处理方法