复制并删除源文件

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');
    }
}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值