码迷,mamicode.com
首页 > 其他好文 > 详细

拷贝一个目录或者文件到指定路径下

时间:2016-03-23 12:56:58      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

/**
* 拷贝一个目录或者文件到指定路径下
*
* @param source
* @param target
*/
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();
}
}
}

 

拷贝一个目录或者文件到指定路径下

标签:

原文地址:http://www.cnblogs.com/lr393993507/p/5310542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!