开发者

Programming an Event listener for files in a directory on Linux

开发者 https://www.devze.com 2022-12-29 11:15 出处:网络
On Ubuntu linux, when you watch a flash video, it gets saved temporarily in the /tmp as flv files while the video buffers. I use vlc to directly play these files.

On Ubuntu linux, when you watch a flash video, it gets saved temporarily in the /tmp as flv files while the video buffers. I use vlc to directly play these files.

Currently, I have scripted a shortcut that directly scans and opens the latest file in /tmp with vlc, when clicked.

But, I want to program a Java application th开发者_C百科at will continually monitor this /tmp directory for any new flv files, and open it in vlc automatically. I know I can use Runtime.exec() to open the VLC application with the flv files. But, I DO NOT want to run a while(true) loop (with sleep) to scan for files.

How can I make use of Event Handling (Java or any other language) on Linux to complete this task?

Edit:

I am also wondering if Java is the right way to approach this. As someone suggested below, Python and QT seem more appropriate.


For Python, use pyinotify: http://trac.dbzteam.org/pyinotify. It's a simple, standalone library; there's no need for an ugly Qt dependency for this.


Have you seen JNotify ? It's a Java library that uses OS-specific code to listen for filesystem events.

I wouldn't rule out polling the file system, however, unless you're dealing with a huge number of files/directories.


In Linux there something called FAM (File Alteration Monitor) which does a better job than the sleep/poll thing. There's a python package for it as well: Python FAM

It is probably going to be a lot less to depend on than for example QT.


I would recommend Qt and Python.

I've used PyQt for a similar project before. Qt has a file system watcher that monitors directories and files for updates, which trigger events that you can catch and do stuff (like open vlc).

QFileSystemWatcher

If this is something you just want to always run in the background, Qt also has a feature that allows you to run your program in the System Tray. This is what I did, and just added a menu or two to make modifications.

QSystemTrayIcon


For Python, you can try this this, I find it simpler than pyinotify.

0

精彩评论

暂无评论...
验证码 换一张
取 消