-3
A
回答
0
public class Listen_Directory {
private final WatchService watcher ;
private final Map<WatchKey,Path> keys;
private final boolean recursive;
private boolean trace = false;
private static PrintWriter writeEvent = null ;
static Interface interf = new Interface();
//private static List<String> listDeletedFiles = new ArrayList<>();
@SuppressWarnings("unchecked")
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>)event;
}
/**
* Register the given directory with the WatchService
*/
private void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher,ENTRY_CREATE,ENTRY_DELETE, ENTRY_MODIFY);
if (trace) {
Path prev = keys.get(key);
if (prev == null) {
System.out.format("register: %s\n", dir);
} else {
if (!dir.equals(prev)) {
System.out.format("update: %s -> %s\n", prev, dir);
}
}
}
keys.put(key, dir);
}
/**
* Register the given directory, and all its sub-directories, with the
* WatchService.
*/
private void registerAll(final Path start) throws IOException {
// register directory and sub-directories
try {
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs)
throws IOException
{
register(dir);
return FileVisitResult.CONTINUE;
}
});
} catch (AccessDeniedException exc){
} catch (FileSystemException ex){
}
}
/**
* Creates a WatchService and registers the given directory
*/
Listen_Directory(Path dir, boolean recursive) throws IOException {
this.watcher = FileSystems.getDefault().newWatchService();
this.keys = new HashMap<WatchKey,Path>();
this.recursive = recursive;
if (recursive) {
System.out.format("Scanning %s ...\n", dir);
registerAll(dir);
System.out.println("Done_"+dir);
} else {
register(dir);
}
// enable trace after initial registration
this.trace = true;
}
/**
* Process all events for keys queued to the watcher
*/
void processEvents() {
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();
System.out.format("%s: \n", event.kind().name());
// TBD - provide example of how OVERFLOW event is handled
// Context for directory entry event is the file name of entry
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
// print out event
System.out.format("%s: %s\n", event.kind().name(), child);
createDataLink();
String fiche = checkIfIsInRecycleBin(child.toString());
writeEvent.append(fiche+ "\n");
writeEvent.flush();
interf.jTextAreaPaths.append(fiche+"\n");
if (kind == OVERFLOW) {
continue;
}
// if directory is created, and watching recursively, then
// register it and its sub-directories
if (recursive /*&& (kind == ENTRY_CREATE)*/) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
}
}
}
// 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;
}
}
}
}
static void usage() {
System.err.println("usage: java WatchDir [-r] dir");
System.exit(-1);
}
public static void main(String[] args) throws IOException {
interf.setVisible(true);
interf.setLocationRelativeTo(null);
File[] listeFichiers = File.listRoots();
for(File leFichier : listeFichiers){
String leChemin = leFichier.getAbsolutePath() ;
//if ((!leChemin.equals("F:\\")) && (!leChemin.equals("D:\\"))
// && (!leChemin.equals("E:\\"))){
Manage_Thread_Directories gestNotif
= new Manage_Thread_Directories(leChemin);
gestNotif.start();
//}
}
//writeEvent.close();
}
private void createDataLink(){
try {
String userLogged = System.getProperty("user.name");
File dir = new File("c:/Users/"+userLogged+"/NotesDeletedFiles");
if(!dir.exists()){
System.out.println(dir.mkdir());
}
File dataFile = new File("c:/Users"
+ "/"+userLogged+"/NotesDeletedFiles/Data.in");
if (!dataFile.exists()){
dataFile.createNewFile();
}
writeEvent = new PrintWriter(new BufferedWriter(new FileWriter(
"c:/Users/"+userLogged+"/NotesDeletedFiles/Data.in", true)));
} catch (IOException ex) {
Logger.getLogger(Listen_Directory.class.getName()).
log(Level.SEVERE, null, ex);
}
}
private String checkIfIsInRecycleBin(String chaine) {
String resu = chaine;
String tab[] = chaine.split("\\\\");
for(int i=0; i<tab.length; i++){
if (tab[i].length() > 2){
String ch = tab[i];
if (ch.startsWith("$REC")){
resu = "Recycle bin" +
"\\"+tab[ tab.length -1 ] ;
break ;
//return resu ;
}
}
}
return resu ;
}
}
公共类Manage_Thread_Directories扩展Thread { 私人字符串cheminAcces = “”;
public Manage_Thread_Directories(String leChemin) {
this.cheminAcces = leChemin ;
}
@Override
public void run() {
String faire = "-r";
if (cheminAcces.length() == 0)
usage();
boolean recursive = false;
if (faire.equals("-r")) {
//if (cheminAcces.length() < 2)
// usage();
recursive = true;
}
// register directory and process its events
Path dir = Paths.get(cheminAcces);
try {
new Listen_Directory(dir, recursive).processEvents();
//super.run();
} catch (IOException ex) {
Logger.getLogger(Manage_Thread_Directories.class.getName()).
log(Level.SEVERE, null, ex);
}
}
}
+0
这些是我制作的源代码。但是当我从中删除文件时,我无法从“C:/”分区获得通知。请,我该如何解决它?谢谢 – Djenh
相关问题
- 1. git-mv显示文件已删除
- 2. Textmate显示已删除的项目文件夹中的文件
- 3. Get-TFSChildItem不显示已删除的文件/文件夹
- 4. Eclipse的Perforce插件显示已删除的文件
- 5. 如何在已删除的文件夹中删除已删除的文件
- 6. Google Picker API显示已删除的文件
- 7. Git在提交后仍然显示已删除的文件
- 8. 当与TFS比较文件时,只显示已删除的行
- 9. CruiseControl.Net已删除的文件
- 10. 显示已删除的子框架(wxPython)
- 11. LookUpEdit显示已删除的项目
- 12. MPMediaPickerController显示已删除的歌曲
- 13. Gridview显示已删除的数据
- 14. 文件夹浏览器对话框中显示的已删除的文件夹
- 15. 如何在Windows文件系统中显示已删除文件的列表
- 16. 使用java删除文件
- 17. 使用java删除文件
- 18. 用Java删除文件
- 19. 如果用户已被删除,请删除用户的文件
- 20. 已删除.eclipse文件夹
- 21. grgit - 添加新文件并删除已删除的文件
- 22. 如何使用Java恢复已删除的文件?
- 23. 用绿色显示新文件,用红色删除文件
- 24. XCode - 从NSDocumentDirectory显示/删除文件
- 25. 从Java文本组件中获取已删除的文本
- 26. Microsoft Graph - 已删除邮件文件夹中的已删除事件
- 27. Java中的文件删除
- 28. 的UIImageView已经显示“已删除”图片
- 29. 的Java书写到已删除的文件
- 30. Java PreparedStatement删除查询:显示删除的行数?
回收站/垃圾桶只是喜欢别人 – peter
你的意思是要列出什么是* *在回收站目录?这只是一个目录,你可以列出它的内容。或者你的意思是你想要列出以前在回收站中已经清空的内容,并且在文件系统级别上实际删除了*。这听起来相当复杂,并且可能从阅读目标文件系统开始,因为并非所有文件系统都会以同样的方式处理删除操作。在这一点上,问题就变成了你正在寻找的实际信息,因为信息*不存在*的可能性越来越大。 – David
请向我们提供您已经编写的代码。 – dckuehn