---
title: "用JAVA实现文件copy的代码示例"
author: "lenj"
date: "2008-05-26T11:14:54Z"
tags: []
slug: "java-copy"
draft: false
lastmod: "2008-05-26T11:14:54Z"
id: 196932
url: "https://lenj.iteye.com/blog/196932"
---

/**
* 拷贝一个文件到另一个目录
*/
public boolean copyFile(String from,String to){

File fromFile,toFile;
fromFile = new File(from);
toFile = new File(to);
FileInputStream fis = null;
FileOutputStream fos = null;

try{
toFile.createNewFile();
fis = new FileInputStream(fromFile);
fos = new FileOutputStream(toFile);
int bytesRead;
byte buf = new byte;// 4K buffer
while((bytesRead=fis.read(buf))!=-1){
fos.write(buf,0,bytesRead);
}
fos.flush();
fos.close();
fis.close();
}catch(IOException e){
System.out.println(e);
return false;
}
return true;

}