import java.io.*;
public class CopyFile2 {
public static void main(String[] args) throws IOException {
copyFileName("E:\\图片", "E:\\");
}
public static void copyFileName(String s1, String s2) throws IOException {
File file1 = new File(s1);
if (!file1.exists()) {
return;
}
String name1 = file1.getName();
if (file1.getParent().equals(s2)) {
if (file1.isFile()) {
String[] split = name1.split(".");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
sb.append(split[i]);
}
name1 = sb.toString() + " - 副本" + split[split.length - 1];
} else {
name1 += " - 副本";
}
}
File file2 = new File(s2 + name1);
copyFiles(file1, file2);
}
public static void copyFiles(File file1, File file2) throws IOException {
if (file1.isFile()) {
copyFile(file1, file2);
return;
}
file2.mkdir();
File[] files = file1.listFiles();
if (files != null) {
for (File file1_1 : files) {
copyFiles(file1_1, new File(file2, file1_1.getName()));
}
}
}
public static void copyFile(File file1, File file2) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
byte[] bytes = new byte[1024];
int read = 0;
while ((read = bis.read(bytes)) != -1) {
bos.write(bytes, 0, read);
}
bis.close();
bos.close();
}
}