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

Posted 魔流剑●风之痕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了拷贝一个目录或者文件到指定路径下相关的知识,希望对你有一定的参考价值。

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

 

以上是关于拷贝一个目录或者文件到指定路径下的主要内容,如果未能解决你的问题,请参考以下文章

python中怎样转到指定目录

求汇编语言将一个内存块拷贝到指定地址的代码

linux文件拷贝到其他机器

QT下编程,有没有把文件从一路径下拷贝到另一路径下的函数,跪求

python中怎样将文件拷贝到指定的目录下?

文件管理命令