Tôi có một ứng dụng mà tôi muốn nghe bất kỳ thay đổi nào được thực hiện cho một thư mục cụ thể. Ứng dụng sẽ ping cho tôi ngay khi có bất kỳ tệp nào được thêm, xóa hoặc cập nhật trong thư mục đó.Trình nghe thư mục trong Java
Trả lời
Bạn có thể sử dụng JNotify
JNotify là một thư viện java cho phép ứng dụng java để lắng nghe để nộp sự kiện hệ thống, chẳng hạn như: File tạo tập tin sửa đổi tập tin đổi tên file bị xóa được hỗ trợ nền tảng
Ghi chú Windows (2000 hoặc mới hơn) Windows Linux với hỗ trợ INofity (2.6.14 hoặc mới hơn) Linux ghi chú Mac OS X (1 0,5 hoặc mới hơn) Mac OS lưu ý
More Info:
Tải JNotify từ here
Giải nén zip, đưa .dll/.so theo nền tảng trong đường dẫn thư mục lib của bạn. và tạo lớp học cung cấp jnotify-0.93.jar
trong đường dẫn lớp học.
Mẫu mã:
package org.life.java.stackoverflow.questions;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
/**
*
* @author Jigar
*/
public class JNotifyDemo {
public void sample() throws Exception {
// path to watch
String path = System.getProperty("user.home");
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED
| JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED
| JNotify.FILE_RENAMED;
// watch subtree?
boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
Thread.sleep(1000000);
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
print("renamed " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
print("modified " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
print("deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name);
}
void print(String msg) {
System.err.println(msg);
}
}
public static void main(String[] args) throws Exception {
new JNotifyDemo().sample();
}
}
Output:
modified C:\Documents and Settings\jigar: LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default
deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea9
created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Current Session
deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea8
created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
Chú thích thông báo tệp trong java. Mã mẫu
public void sample() throws Exception {
// path to watch
String path = System.getProperty("user.home");
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED |
JNotify.FILE_DELETED |
JNotify.FILE_MODIFIED |
JNotify.FILE_RENAMED;
// watch subtree? boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
Thread.sleep(1000000);
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
}
class Listener implements JNotifyListener
{
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
print("renamed " + rootPath + " : " + oldName + " -> " + newName); }
public void fileModified(int wd, String rootPath, String name)
{ print("modified " + rootPath + " : " + name); }
public void fileDeleted(int wd, String rootPath, String name) {
print("deleted " + rootPath + " : " + name); }
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name); }
void print(String msg) {
System.err.println(msg); }
}
Khi tôi chạy chương trình nó mang lại cho tôi exceptionError tải thư viện, java.library.path = C: \ Program Files \ Java \ jdk1.7.0 \ bin;.; C: \ Windows \ Sun \ Java \ bin; C: \ Windows \ system32; C: \ Windows; C: \ Windows; C: \ Windows \ system32; C: \ Windows \ system32 \ Wbem; \ WindowsPowerShell \ v1.0 \; C: \ Program Files \ Microsoft SQL Server \ 90 \ Công cụ \ binn \; C: \ apache-tomcat-6.0.26 \ bin; C: \ Program Files \ Java \ jdk1 .7.0 \ bin; C: \ Program Files \ TortoiseSVN \ bin; C: \ Program Files \ putty; C: \ Program Files \ Google \ Chrome \ Application; C: \ Program Files \ Java \ jdk1.7.0 \ include Ngoại lệ trong chuỗi "main" java.lang.Unsa – Jinith
1.- Nhấp chuột phải vào Dự án 2.- Thuộc tính 3.- Nhấp vào RUN 4.- Tùy chọn VM: java -Djava.library.path = "your_path" 5.- ví dụ trong trường hợp của tôi: java -Djava.library.path = Người dùng/Lexynux/NetBeansProjects/NAO/libs> 6.- Ok –
Kể từ Java 1,7 bạn có thể sử dụng Watch Service API để đăng ký cho các sự kiện thư mục. Nó là một phần của thư viện New I/O (NIO) của Java và không yêu cầu bất kỳ tài nguyên bổ sung nào. Một ví dụ về cách sử dụng API có thể được tìm thấy trong số official documentation.
Sau khi đăng ký các WatchService bạn có thể lấy các sự kiện cho đường dẫn mục tiêu như thế này:
for (WatchEvent<?> event: key.pollEvents()) {
// 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);
}
- 1. Trình nghe thư mục trong C#
- 2. Trình nghe khóa Java trong Commandline
- 3. Tạo trình nghe tùy chỉnh Trong Java
- 4. Trình nghe hành động Java
- 5. Trình nghe Java trên hộp thoại đóng
- 6. Trình nghe nhấp chuột đôi trên JTable trong Java
- 7. Java - Tìm kiếm tệp trong thư mục
- 8. Xóa thư mục khi thoát trong Java
- 9. Cách tạo thư mục trống trong java?
- 10. Chọn đích thư mục trong Java?
- 11. Thư viện Picasso, Android: Sử dụng Trình nghe Lỗi
- 12. xóa thư mục từ java
- 13. Người nghe sự kiện trong Java
- 14. Cách lấy danh sách các tệp/thư mục của url thư mục trong java?
- 15. Trình nghe Java phải kế thừa từ java.util.EventListener - tại sao?
- 16. Mở trình chọn thư mục trong C#
- 17. Trình nghe tạo chủ đề
- 18. Sự kiện Java và trình nghe sự kiện
- 19. Làm cách nào để tạo thư mục trong thư mục làm việc hiện tại, trong Java
- 20. Android, Trình nghe hộp kiểm trong XML?
- 21. Tạo tệp trong tài nguyên/thư mục nguồn trong java theo lập trình?
- 22. Thư viện trình duyệt Java
- 23. java: Mở thư mục trên nút bấm
- 24. Trình nghe nhạc
- 25. Cách lấy đường dẫn thư mục thực hiện trong java
- 26. Trình nghe mạng Android
- 27. Java - Nghe thay đổi biến
- 28. Trình lắng nghe chrome.webRequest.onAuthRequired
- 29. Java tải xuống tất cả các tệp và thư mục trong một thư mục
- 30. Tạo lại cấu trúc thư mục bên trong tệp Zip bằng Java - Các thư mục trống
* Downvoters * xin vui lòng bình luận –
Khi tôi chạy chương trình nó mang lại cho tôi exceptionError tải thư viện, java.library.path = C : \ Program Files \ Java \ jdk1.7.0 \ bin;.; C: \ Windows \ Sun \ Java \ bin; C: \ Windows \ system32; C: \ Windows; C: \ Windows; C: \ Windows \ system32; C: \ Windows \ system32 \ Wbem; \ WindowsPowerShell \ v1.0 \; C: \ Program Files \ Microsoft SQL Server \ 90 \ Công cụ \ binn \; C: \ apache-tomcat-6.0.26 \ bin; C: \ Tệp chương trình \ Java \ jdk1.7.0 \ bin; C: \ Program Files \ TortoiseSVN \ bin; C: \ Program Files \ putty; C: \ Program Files \ Google \ Chrome \ Application; C: \ Program Files \ Java \ jdk1.7.0 \ include Ngoại lệ trong chuỗi "main" java.lang.Unsa – Jinith
'Giải nén zip, đặt .dll/.so theo nền tảng trong đường dẫn lib của bạn.' –