package readPhoto;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class CopyFile {
static CopyFile copyFile = new CopyFile();
public static void main(String[] args) throws Exception {
File file = new File(“D://jkk_img”);
check(file);
delete(file);
}
public static void delete(File file) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory() && f.getName().length()!=4) {
copyFile.deleteFile(f);
} else if (f.isFile()){
f.delete();
}
}
}
public static void check(File file) throws Exception {
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory()) {
check(f);
} else {
System.out.println(f.getName());
// movePhoto.readFile(f);
// movePhoto.copy(f);
String filename = f.getName();
String year = filename.substring(6, 10);
System.out.println(year);
String birthday = filename.substring(10, 14);
System.out.println(birthday);
File f1 = new File("D://jkk_img//" + year + "//" + birthday);
if (!f1.exists()) {
f1.mkdirs();
}
System.out.println(f.getAbsolutePath());
System.out.println(f1.getAbsolutePath());
copyFile.copyFile(f.getAbsolutePath(), f1.getAbsolutePath());
}
}
}
public static void copyFile(String sorucePath, String targetDir)
throws IOException {
// 文件名
File f = new File(sorucePath);
InputStream zis = new FileInputStream(sorucePath);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(targetDir, f.getName()));
byte[] buff = new byte[1024];
int len;
while ((len = zis.read(buff)) > 0) {
fos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
fos.close();
}
if (zis != null) {
zis.close();
}
}
}
private void deleteFile(File file) {
if (file.exists()) {
// 判断文件是否存在
if (file.isFile()) {
// 判断是否是文件
file.delete();
// delete()方法 你应该知道 是删除的意思;
} else if (file.isDirectory()) {
// 否则如果它是一个目录
File files[] = file.listFiles();
// 声明目录下所有的文件 files[];
for (int i = 0; i < files.length; i++) {
// 遍历目录下所有的文件
this.deleteFile(files[i]);
// 把每个文件 用这个方法进行迭代搜索
}
}
file.delete();
} else {
System.out.println("所删除的文件不存在!" + '\n');
}
}
}