views:

277

answers:

5

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 that 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.

+1  A: 

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.

Brian Agnew
@Brian I'll certainly have a look. By the way, isn't polling via while(true) considered to be a bad practice? There are only half a dozen files to monitor. I am also wondering if Java is a suitable language for this purpose.
Epitaph
I think polling can be a pragmatic solution, so long as you're not polling so hard as to affect your disk performance. Polling (say) once a second won't impact your system to any noticeable degree.
Brian Agnew
It will if there happen to be a lot of other files in /tmp.
Glenn Maynard
Can you qualify how many ? I would expect performance to degrade when you have thousands of files (perhaps 10s of thousands).
Brian Agnew
A: 

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

Brendan Abel
Thanks Brendan. I have minimum experience with PyQt, but am interested to try it out. Are there any resources/books that you would like to recommend from your experience?
Epitaph
A Qt dependency just to monitor file events would be terrible.
Glenn Maynard
+2  A: 

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.

Glenn Maynard
A: 

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

dugres
+1  A: 

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.

Mattias Nilsson