欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个备份文件的程序思路

程序员文章站 2022-06-29 13:29:33
...

1.空目录不备份,但非空目录都备份

2.源目录 source 要递归他下面所有的文件和目录 存入List

3.循环这个list,创建每个文件的目录

4.复制

public class Test12 {

    public static void main(String[] args) throws InterruptedException {

        Timer t = new Timer();
        t.scheduleAtFixedRate(new MyTask(),new Date(),1000*30);

        for(int i = 0;i<10000;i++){
            Thread.sleep(1000);
            System.out.println("warning");
        }


    }

}

class MyTask extends TimerTask{

    static final String SOURCE = "e:\\a";
    static String DEST;

    @Override
    public void run() {

        Date d = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        DEST = "e:\\dest_" + df.format(d);
        File file = new File(SOURCE);
        File dest = new File(DEST);

        if(dest.exists()){
            deleteAll(dest);
        }
        System.out.println("创建备份目录?"+dest.mkdirs());
        List<File> list = new ArrayList<File>();

        getAllFile(file,list);

        backUp(list,dest);
    }

    //备份
    private static void backUp(List<File> list, File dest) {

        if (list == null || list.size() <= 0) {
            return;
        }
        for (File f : list) {
            String fpath = f.getAbsolutePath();     //取出绝对路径        e:\\a
            String newpath = fpath.replace(SOURCE, DEST);
            System.out.println("对应的新路径" + newpath);
            File newFile = new File(newpath);
            if (newFile.getParentFile().exists() == false) {
                System.out.println("创建" + newFile + "的父目录成功?" + newFile.getParentFile().mkdirs());
            }
            if (f.isFile()) {
                copy(f, newFile);
            }
        }

    }

    //复制
    private static void copy(File inFile, File outFile) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        boolean isFlag = false;

        try {
            fis = new FileInputStream(inFile);
            fos = new FileOutputStream(outFile);

            byte[] bs = new byte[1024];
            int length = -1;
            while ((length = fis.read(bs)) != -1) {
                fos.write(bs, 0, length);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //递归获取原目录下所有的文件信息
    private static void getAllFile(File source, List<File> list) {
        if (source.isDirectory()) {
            //查看子目录 listFile()
            File[] fs = source.listFiles();
            if (fs != null && fs.length > 0) {
                //说明有子目录或子文件
                for (File ff : fs) {
                    getAllFile(ff, list);
                }
            }

        }
        list.add(source);

    }

    //递归删除
    private static void deleteAll(File f) {
        if (f.isDirectory()) {
            File[] fs = f.listFiles();
            if (fs != null && fs.length > 0) {
                for (File file : fs) {
                    deleteAll(file);
                }
            }
        }
        System.out.println(f + "删除成功?" + f.delete());
    }

}