java 实现文件监控
来源:互联网 发布:国家旅游局2016年数据 编辑:程序博客网 时间:2024/05/01 15:45
public class MainFrame extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 543, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("监控路径:");
label.setBounds(33, 20, 65, 15);
contentPane.add(label);
textField = new JTextField("D:/");
textField.setBounds(90, 16, 219, 21);
contentPane.add(textField);
textField.setColumns(10);
JButton button = new JButton("开始监控");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
addWatch();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
button.setBounds(319, 16, 93, 23);
contentPane.add(button);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(33, 45, 480, 207);
contentPane.add(scrollPane);
}
public void addWatch() throws Exception {
String path = textField.getText();
int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
boolean watchSubtree = true;
//添加文件监听
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
textArea.append("文件:" + rootPath + " : " + oldName + " 重命名为: "
+ newName + "\n");
}
public void fileModified(int wd, String rootPath, String name) {
textArea.append("文件修改 " + rootPath + " : " + name + "\n");
}
public void fileDeleted(int wd, String rootPath, String name) {
textArea.append("删除文件: " + rootPath + " : " + name + "\n");
}
public void fileCreated(int wd, String rootPath, String name) {
textArea.append("新建文件: " + rootPath + " : " + name + "\n");
}
}
}
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 543, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("监控路径:");
label.setBounds(33, 20, 65, 15);
contentPane.add(label);
textField = new JTextField("D:/");
textField.setBounds(90, 16, 219, 21);
contentPane.add(textField);
textField.setColumns(10);
JButton button = new JButton("开始监控");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
addWatch();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
button.setBounds(319, 16, 93, 23);
contentPane.add(button);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(33, 45, 480, 207);
contentPane.add(scrollPane);
}
public void addWatch() throws Exception {
String path = textField.getText();
int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
boolean watchSubtree = true;
//添加文件监听
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
textArea.append("文件:" + rootPath + " : " + oldName + " 重命名为: "
+ newName + "\n");
}
public void fileModified(int wd, String rootPath, String name) {
textArea.append("文件修改 " + rootPath + " : " + name + "\n");
}
public void fileDeleted(int wd, String rootPath, String name) {
textArea.append("删除文件: " + rootPath + " : " + name + "\n");
}
public void fileCreated(int wd, String rootPath, String name) {
textArea.append("新建文件: " + rootPath + " : " + name + "\n");
}
}
}
- java 实现文件监控
- java实现文件监控
- java实现文件监控
- Java实现文件监控之一
- java 实现监控某目录文件变化
- 利用Java NIO 实现文件监控功能。
- Java实现文件监控之二
- Java技巧:java实现文件监控技巧分享
- Java 文件监控,实时监控文件加载
- Java 文件监控,实时监控文件加载
- java实现文件实时监控:Jnotify和FileAlteration
- java文件监控
- Java监控文件变化
- JAVA轻量级文件监控
- java监控目录文件
- Java监控文件变化
- JAVA 文件监控 WatchService
- java实现远程桌面监控
- QT3.2.1 与 VC6.0的集成
- 初学LINUX命令集合(必须掌握哦)
- 页面乱码问题根源浅析
- 信号量与自旋锁 |Linux,信号量,自旋锁,睡眠锁,spinlock,semaphore
- hdu 2845 Beans
- java 实现文件监控
- loadView和viewDidLoad/viewDidUnload及initWithNibName/awakeFromNib/initWithCoder理解
- XML Publisher 开发相关
- Android SDcard中的文件夹大解析!
- 让windows explorer文件浏览窗口左侧默认显示目录树
- 通过oralce的内置函数查询树形结构的结果
- GUI 资源库
- Android调用.NET Webservice报org.ksoap2.serialization.SoapPrimitive误
- 从零开始学习jQuery (一) 开天辟地入门篇