您的位置:首页 > 其它

文件操作(把源文件拷贝到目标文件路径下)

2012-06-30 14:16 239 查看
/**
* 功能描述:拷贝一个目录或者文件到指定路径下,即把源文件拷贝到目标文件路径下
*
* @param source
*            源文件
* @param target
*            目标文件路径
* @return void
*/
public static void copy(File source, File target) {
File tarpath = new File(target, source.getName());
if (source.isDirectory()) {
tarpath.mkdir();
File[] dir = source.listFiles();
for (int i = 0; i < dir.length; i++) {
copy(dir[i], tarpath);
}
} else {
try {
InputStream is = new FileInputStream(source); // 用于读取文件的原始字节流
OutputStream os = new FileOutputStream(tarpath); // 用于写入文件的原始字节的流
byte[] buf = new byte[1024];// 存储读取数据的缓冲区大小
int len = 0;
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
is.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐