Java技巧:java实现文件监控技巧分享
2011-01-19 13:09
609 查看
java本身不能直接监听系统的文件操作事件,不过可以先编写C/C++调用操作系统的API监听文件,再通过jni调用的方式实现。限于本人的C/C++水平有限,没有用C/C++实现该接口,而且已有开源组件JNotify实现了这个功能,本文例子使用JNotify。
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");
}
}
}
运行效果:在D盘新建一个文件和修改文件名操作。
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");
}
}
}
运行效果:在D盘新建一个文件和修改文件名操作。
相关文章推荐
- Java实现监控目录下文件变化
- java实现远程连接服务器,监控某个目录下的日志文件
- java 使用线程监控文件目录变化的实现方法
- java 使用线程监控文件目录变化的实现方法
- Java实现文件监控之一
- java jsch实现sftp文件上传,并且控制上传的速度,同时监控上传进度
- java实现文件变化监控
- java实现文件变化监控的方法(推荐)
- PHP写入文件实现技巧分享
- java实现文件监控
- java 实现监控某目录文件变化
- 分享一个java对xml,excel,jdbc.properties,读写文件,读写图片等实现(3)
- java实现文件实时监控:Jnotify和FileAlteration
- Java Web端程序实现文件下载的方法分享
- 分享一个java对xml,excel,jdbc.properties,读写文件,读写图片等实现(2)
- 分享一个java对xml,excel,jdbc.properties,读写文件,读写图片等实现(1)
- Java实现文件监控之二
- java实现倒序读取文件功能示例分享
- java实现文件监控
- java实现文件监控