package ca.filemover;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
public class CAWatcher {
public static void main(String[] args) {
System.out.println("Scan the combat arms folder...");
while (true) {
Path path = Paths.get("C:\\Nexon\\Combat Arms EU");
try {
WatchService watcher = path.getFileSystem().newWatchService();
path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
WatchKey watckKey = watcher.take();
List<WatchEvent<?>> events = watckKey.pollEvents();
for (WatchEvent<?> event : events) {
String fileName = event.context().toString();
String filePath = path.toString();
String source = filePath + "\\" + fileName;
while (true) {
if (event.context().toString().contains(".avi")) {
if (isFileUnlocked(source)) {
changeFileDest(source, fileName);
break;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static boolean isFileUnlocked(String sourceFile) {
File fileLoc = new File(sourceFile);
return fileLoc.renameTo(fileLoc);
}
public static boolean changeFileDest(String source, String fileName) {
Path moveSourcePath = Paths.get(source);
Path moveTargetPath = Paths.get("D:\\Desktop\\CA Videos\\Reports\\" + fileName);
try {
Files.move(moveSourcePath, moveTargetPath);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
java.nio.file.FileAlreadyExistsException:D:\ Desktop \ CA. 视频\报告\ CA_2015_03_30_42.avi at sun.nio.fs.WindowsFileCopy.move(未知来源)at sun.nio.fs.WindowsFileSystemProvider.move(未知来源)at java.nio.file.Files.move(未知来源)at ca.filemover.CAWatcher.changeFileDest(CAWatcher.java:73)at ca.filemover.CAWatcher.main(CAWatcher.java:44)
提前致谢。