com.spun.util.io.FileMonitor Maven / Gradle / Ivy
The newest version!
package com.spun.util.io;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
public class FileMonitor
{
private Timer timer;
private Map files;
private Set listeners;
public FileMonitor(long pollingInterval)
{
files = new HashMap();
listeners = new HashSet();
timer = new Timer(true);
timer.schedule(new FileMonitorNotifier(), 0, pollingInterval);
}
public void stop()
{
timer.cancel();
}
public void addFile(File file)
{
if (!files.containsKey(file))
{
files.put(file, (file.exists() ? file.lastModified() : -1L));
}
}
public void removeFile(File file)
{
files.remove(file);
}
public void addListener(FileListener fileListener)
{
listeners.add(fileListener);
}
public void removeListener(FileListener fileListener)
{
listeners.remove(fileListener);
}
private class FileMonitorNotifier extends TimerTask
{
public void run()
{
for (File file : files.keySet())
{
long lastModifiedTime = ((Long) files.get(file)).longValue();
long newModifiedTime = file.exists() ? file.lastModified() : -1;
if (newModifiedTime != lastModifiedTime)
{
files.put(file, newModifiedTime);
for (FileListener listener : listeners)
{
listener.fileChanged(file);
}
}
}
}
}
public interface FileListener
{
void fileChanged(File file);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy