/**
* 拷贝一个目录或者文件到指定路径下
*
* @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();
}
}
}
拷贝文件到指定目录
最新推荐文章于 2023-05-17 16:05:06 发布
本文介绍了一个用于拷贝文件或整个目录的方法实现。该方法通过递归遍历源目录下的所有文件及子目录,并将它们复制到目标路径下。对于文件复制部分,采用字节流方式逐块读取并写入,确保数据完整转移。

6326

被折叠的 条评论
为什么被折叠?



