import java.io.*;
import java.util.Date;
public class FindingFiles {
public static void main(String[] args) throws Exception {
File dir = new File("C:\\Users\\Mayank\\Desktop\\java princeton");// Directory that contain the files to be searched
File[] files= dir.listFiles();
File des=new File("C:\\Users\\Mayank\\Desktop\\newDir"); // new directory where files are to be moved
if(!des.exists())
des.mkdir();
for(File f : files ){
long diff = new Date().getTime() - f.lastModified();
if( (f.length() >= 10485760) || (diff > 10 * 24 * 60 * 60 * 1000) )
{
f.renameTo(new File("C:\\Users\\mayank\\Desktop\\newDir\\"+f.getName()));
} }
}
}
答案 0 :(得分:3)
观看服务应符合您的需求:https://docs.oracle.com/javase/tutorial/essential/io/notification.html
以下是实施观看服务所需的基本步骤:
作为旁注,您应该支持自Java 7以来可用的java.nio
包来移动文件。
renameTo()
有一些严重的限制(从Javadoc中提取):
此方法行为的许多方面都是固有的 依赖于平台:重命名操作可能无法移动 从一个文件系统到另一个文件系统的文件,它可能不是原子的,它 如果文件具有目标抽象路径名,则可能不会成功 已经存在。应始终检查返回值以确保 重命名操作成功。
例如,在Windows上,如果目标目录存在,renameTo()
可能会失败,即使它是空的。
此外,该方法可能在失败时返回布尔值而不是异常
通过这种方式,可能很难猜出问题的根源并在代码中处理它。
这是一个执行您的要求的简单类(它处理所有步骤,但不一定需要Watch Service关闭)。
package watch;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class WatchDir {
private final WatchService watcher;
private final Map<WatchKey, Path> keys;
private Path targetDirPath;
private Path sourceDirPath;
WatchDir(File sourceDir, File targetDir) throws IOException {
this.watcher = FileSystems.getDefault().newWatchService();
this.keys = new HashMap<WatchKey, Path>();
this.sourceDirPath = Paths.get(sourceDir.toURI());
this.targetDirPath = Paths.get(targetDir.toURI());
WatchKey key = sourceDirPath.register(watcher, ENTRY_CREATE);
keys.put(key, sourceDirPath);
}
public static void main(String[] args) throws IOException {
// Directory that contain the files to be searched
File dir = new File("C:\\Users\\Mayank\\Desktop\\java princeton");
// new directory where files are to be moved
File des = new File("C:\\Users\\Mayank\\Desktop\\newDir");
if (!des.exists())
des.mkdir();
// register directory and process its events
new WatchDir(dir, des).processEvents();
}
/**
* Process all events for keys queued to the watcher
*
* @throws IOException
*/
private void processEvents() throws IOException {
for (;;) {
// wait for key to be signalled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
Path dir = keys.get(key);
if (dir == null) {
System.err.println("WatchKey not recognized!!");
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
// Context for directory entry event is the file name of entry
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path name = ev.context();
Path child = dir.resolve(name);
// print out event
System.out.format("%s: %s\n", event.kind().name(), child);
// here is a file or a folder modified in the folder
File fileCaught = child.toFile();
// here you can invoke the code that performs the test on the
// size file and that makes the file move
long diff = new Date().getTime() - fileCaught.lastModified();
if (fileCaught.length() >= 10485760 || diff > 10 * 24 * 60 * 60 * 1000) {
System.out.println("rename done for " + fileCaught.getName());
Path sourceFilePath = Paths.get(fileCaught.toURI());
Path targetFilePath = targetDirPath.resolve(fileCaught.getName());
Files.move(sourceFilePath, targetFilePath);
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
keys.remove(key);
// all directories are inaccessible
if (keys.isEmpty()) {
break;
}
}
}
}
}