import java.io.*;
public class StrCopy {
public static void main(String[] args) {
File oldFile=new File("E:\\英雄时刻");
File newFile=new File("C:\\");
copy(oldFile,newFile);
}
private static void copy(File oldFile, File newFile) {
File[] files=oldFile.listFiles();
if(oldFile.isFile()){
FileInputStream in=null;
FileOutputStream out=null;
try {
in=new FileInputStream(oldFile);
String path=(newFile.getAbsolutePath().endsWith("\\")?newFile.getAbsolutePath():newFile.getAbsolutePath()+"\\")+oldFile.getAbsolutePath().substring(3);
out=new FileOutputStream(path);
byte[]b=new byte[1024*1024];
int count;
while((count=in.read(b))!=-1){
out.write(b,0,count);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
for(File file:files){
if(file.isDirectory()){
String oldDir=file.getAbsolutePath();
String newDir=(newFile.getAbsolutePath().endsWith("\\")?newFile.getAbsolutePath():newFile.getAbsolutePath()+"\\")+oldDir.substring(3);
System.out.println(newDir);
File newfile=new File(newDir);
if (!newfile.exists()) {
newfile.mkdirs();
}
}
copy(file,newFile );
}
}
}