我也写了个,大家参考下public class Test33 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
File res= new File("E:\\1");
File des = new File("F:");
copyFile(res,des);
}
public static void copyFile(File res,File des) throws IOException
{
if(!des.isDirectory())
throw new RuntimeException("非法文件路径");
if(res.isDirectory())
{
File creatFile = new File(des.getAbsolutePath(),res.getName());
creatFile.mkdir();
File[] files = res.listFiles();
for(File file : files)
{
if(file.isDirectory())
{
copyFile(file,creatFile);
}
else
copyTxt(file,new File(creatFile,file.getName()));
}
}
else
copyTxt(res,new File(des,res.getName()));
}
public static void copyTxt(File res,File des) throws IOException
{
BufferedReader bufr = new BufferedReader(new FileReader(res));
BufferedWriter bufw = new BufferedWriter(new FileWriter(des));
String line = null;
while((line=bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
bufw.flush();
}
bufr.close();
bufw.close();
}
}
|