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秒执行,目标目录文件不存在,创建。存在,目标目录文件名不同,更名。文件同步复制。效果。保证指定得远地址下的目录和文件同指定目标地址下的目录和文件