用JAVA实现文件copy的代码示例

/**
* 拷贝一个文件到另一个目录
*/
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[4 * 1024];// 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;

}

你可能感兴趣的:(java)