定时同步目录文件

1 篇文章 0 订阅
import java.awt.GridLayout;
import java.io.File;
import java.util.Timer;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JTextField;
public class MyTimerTask extends JFrame {
    public void sys(String path, String _path) {
        File file = new File(path);
        File _file = new File(_path);
        File $file = new File(_file.getParent(), file.getName());
        boolean b = file.isDirectory() && _file.isDirectory() && !file.getName().equals(_file.getName()) && _file.renameTo($file);
        if (b) {
            _file = $file;
        }
        SysDirTask sysDirTask = new SysDirTask(file.getPath(), _file.getPath());
        SysFileTask sysFileTask = new SysFileTask(path, _path);
        Timer timer = new Timer();
        timer.schedule(sysDirTask, 1000, 3000);
        timer.schedule(sysFileTask, 1000, 3000);
    }
    public MyTimerTask() {
        JTextField field = new JTextField(100);
        JTextField _field = new JTextField(100);
        JButton button = new JButton("同步");
        JButton _button = new JButton("清空");
        add(field);
        add(_field);
        add(button);
        add(_button);
        button.addActionListener((e) -> {
            String path = field.getText();
            String _path = _field.getText();
            sys(path, _path);
        });
        _button.addActionListener((e) -> {
            field.setText(null);
            _field.setText(null);
        });
        setLayout(new GridLayout(2, 2));
        setBounds(300, 300, 300, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MyTimerTask();
    }
}
import java.io.File;
import java.util.Date;
import java.util.TimerTask;
public class SysDirTask extends TimerTask {
    private String path;
    private String _path;
    public SysDirTask(String path, String _path) {
        this.path = path;
        this._path = _path;
    }
    @Override
    public void run() {
        task(path, _path);
    }
    public void task(String path, String _path) {
        File file = new File(path);
        File _file = new File(_path);
        File[] files = file.listFiles((dir, name) -> {
            File f = new File(dir, name);
            return f.isDirectory();
        });
        File[] _files = _file.listFiles((dir, name) -> {
            File f = new File(dir, name);
            return f.isDirectory();
        });
        if (files == null || _files == null) {
            return;
        }
        if (_files.length == 0) {
            String $path = "D:\\dir";
            File $file = new File($path);
            File[] $files = $file.listFiles((dir, name) -> {
                File f = new File(dir, name);
                return f.isDirectory() && f.list().length == 0;
            });
            int len = files.length - _files.length;
            for (int i = 0, j = 0; i < len && j < $files.length && $files[j].renameTo(new File(_path, $files[j].getName())); i++, j++) {
                System.out.println(new Date() + ":等待目录加入");
            }
        }
        for (int i = 0, j = 0; i < files.length && j < _files.length && !files[i].getName().equals(_files[j].getName()) && _files[j].renameTo(new File(_files[j].getParent(), files[i].getName())); i++, j++) {
            System.out.println(new Date() + ":目录已替换");
        }
        for (int i = 0, j = 0; i < files.length && j < _files.length; i++, j++) {
            System.out.println(new Date() + ":目录准备替换");
            path = files[i].getPath();
            _path = _files[j].getPath();
            task(path, _path);
        }
    }
}
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.TimerTask;
public class SysFileTask extends TimerTask {
    private String path;
    private String _path;
    public SysFileTask(String path, String _path) {
        this.path = path;
        this._path = _path;
    }
    @Override
    public void run() {
        try {
            sysFiles(path, _path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void sysFiles(String path, String _path) throws IOException {
        File file = new File(path);
        File _file = new File(_path);
        if (file.isDirectory() && _file.isDirectory()) {
            File[] files = file.listFiles((dir, name) -> {
                File f = new File(dir, name);
                return f.isFile();
            });
            File[] _files = _file.listFiles((dir, name) -> {
                File f = new File(dir, name);
                return f.isFile();
            });
            if (_files.length == 0) {
                String $path = "D:\\TEMP";
                File $file = new File($path);
                File[] $files = $file.listFiles((dir, name) -> {
                    File f = new File(dir, name);
                    return f.isFile() && name.startsWith("_");
                });
                int len = files.length - _files.length;
                for (int i = 0, j = 0; i < len && j < $files.length && $files[j].renameTo(new File(_path, $files[j].getName())); i++, j++) {
                    System.out.println(new Date() + ":等待文件加入");
                }
            }
            File $file;
            for (int i = 0, j = 0; i < files.length && j < _files.length && !files[i].getName().equals(_files[j].getName()) && _files[j].renameTo($file = new File(_files[j].getParent(), files[i].getName())); i++, j++) {
                System.out.println(new Date() + ":文件已替换");
            }
            for (int i = 0, j = 0; i < files.length && j < _files.length; i++, j++) {
                $file = new File(_files[j].getParent(), files[i].getName());
                path = files[i].getPath();
                _path = $file.getPath();
                Files.copy(Paths.get(path), Paths.get(_path), StandardCopyOption.REPLACE_EXISTING);
                System.out.println(new Date() + ":文件替换了");
            }
            files = file.listFiles((dir, name) -> {
                File f = new File(dir, name);
                return f.isDirectory();
            });
            _files = _file.listFiles((dir, name) -> {
                File f = new File(dir, name);
                return f.isDirectory();
            });
            if (files == null || _files == null) {
                return;
            }
            for (int i = 0, j = 0; i < files.length && j < _files.length; i++, j++) {
                System.out.println(new Date() + ":文件准备替换");
                sysFiles(files[i].getPath(), _files[j].getPath());
            }
        }
    }
}

每隔3秒周期调度,延迟1秒执行,目标目录文件不存在,创建。存在,目标目录文件名不同,更名。文件同步复制。效果。保证指定得远地址下的目录和文件同指定目标地址下的目录和文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

car-java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值