62,614
社区成员
发帖
与我相关
我的任务
分享
public void run() {
File dir = new File(srcdir);
if (!dir.isDirectory()) {
System.out.println("not directory");
return;
}
Set<File> originalFiles = new HashSet<File>();
for (File f : dir.listFiles()) {
orignalFiles.add(f);
}
Set<File> newFiles = new HashSet<File>();
while (running) {
try {
newFiles.clear();
for (File f : dir.listFiles()) {
if (!originalFiles.contains(f)) {
newFiles.add(f);
}
}
if (newFiles.size() > 0) {
StringBuilder cmd = new StringBuilder("move /Y ");
for (File f : newFiles) {
cmd.append(f.getAbsoluteFile().getPath()).append(" ");
}
cmd.append(targetdir + "/.");
Process p = Runtime.getRuntime().exec(cmd.toString());
p.waitFor();
} else {
sleep(1000);
}
} catch (Exception e) {e.printStackTrace();}
}
}
class MonitorThread extends Thread {
String srcdir;
String targetdir;
boolean running = true;
public MonitorThread(String srcdir, String targetdir) {
this.srcdir = srcdir;
this.targetdir = targetdir;
}
public void stop() {this.running = false;}
public void run() {
File dir = new File(srcdir);
while (running) {
try {
if (!dir.isDirectory()) {
System.out.println("not directory");
return;
}
if (dir.listFiles().length() > 0) {
Process p = Runtime.getRuntime().exec("move /Y " + srcdir + "/*.* " + targetdir + "/.");
p.waitFor();
} else {
sleep(1000);
}
} catch (Exception e) {e.printStackTrace();}
}
}
}