将一个文件夹下所有文件,包括子文件夹,复制到另一个文件夹内。
class FileCopy
{
public void filecopy(String f1, String f2)//将f1中的内容复制到f2
{
File F1 = new File(f1);
File F2 = new File(f2);
try(
FileInputStream fi = new FileInputStream(F1);
FileOutputStream fo = new FileOutputStream(F2);
)
{
byte[] b = new byte[(int)F1.length()];
fi.read(b);
fo.write(b);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void foldercopy(String f1,String d2)//复制全部f1下的文件(及子文件夹和其中的全部文件)到d2下
{
File f = new File(d2);
File F1 = new File(f1);
if(!f.exists())
{
f.mkdir();
}
File[] files = F1.listFiles();
for(File F:files)
{
if(F.isFile())
{
System.out.println(F.getAbsolutePath());
this.filecopy(F.getAbsolutePath(),d2+"\\"+F.getName());
}
if(F.isDirectory())
{
foldercopy(F.getAbsolutePath(),d2+"\\"+F.getName());
}
}
}
}