您的位置:首页 > 其它

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

2008-08-31 12:46 483 查看
**
* 拷贝一个目录或者文件到指定路径下
*
* @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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐