import java.util.*;
import java.io.*;
class CopyFiles
{
public static void main(String[] args) throws Exception
{
File file = new File("D:\\BaiduMusic");
File destFile = new File("F:\\迅雷下载");
copyFile(file,destFile);
System.out.println("传输完成");
}
private static void copyFile(File srcFile,File destFile) throws Exception
{
if(!destFile.exists())
{
destFile.mkdir();
}
File[] files = srcFile.listFiles();
for(File file:files)
{
if(file.isFile())
{
String srcFileName = file.getName();
File aDestFile = new File(destFile,srcFileName);
copy(file,aDestFile);
}
else if(file.isDirectory())
{
String srcDir = srcFile.getAbsolutePath()+File.separator+file.getName();
String destDir = destFile.getAbsolutePath()+File.separator+file.getName();
copyDir(srcDir,destDir);
}
}
}
private static void copy(File file,File destFile) throws Exception
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int len = 0;
byte[] bytes = new byte[1024];
while((len=bis.read(bytes))!=-1)
{
bos.write(bytes,0,len);
}
}
private static void copyDir(String srcDir,String destDir) throws Exception
{
File destFile = new File(destDir);
destFile.mkdirs();
File srcFile = new File(srcDir);
copyFile(srcFile,destFile);
}
}
|
|