本文整理汇总了Java中org.apache.commons.io.monitor.FileAlterationListener类的典型用法代码示例。如果您正苦于以下问题:Java FileAlterationListener类的具体用法?Java FileAlterationListener怎么用?Java FileAlterationListener使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileAlterationListener类属于org.apache.commons.io.monitor包,在下文中一共展示了FileAlterationListener类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addFileAlterationObserver
import org.apache.commons.io.monitor.FileAlterationListener; //导入依赖的package包/类
/**
* Add {@link org.apache.commons.io.monitor.FileAlterationMonitor}s for the given
* root directory and any nested subdirectories under the root directory to the given
* {@link org.apache.commons.io.monitor.FileAlterationMonitor}.
*
* @param monitor a {@link org.apache.commons.io.monitor.FileAlterationMonitor}
* @param listener a {@link org.apache.commons.io.monitor.FileAlterationListener}
* @param rootDir root directory
*/
public static void addFileAlterationObserver(FileAlterationMonitor monitor, FileAlterationListener listener,
File rootDir) {
// Add a observer for the current root directory
FileAlterationObserver observer = new FileAlterationObserver(rootDir);
observer.addListener(listener);
monitor.addObserver(observer);
// List subdirectories under the current root directory
File[] subDirs = rootDir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
if (subDirs == null || subDirs.length == 0) {
return;
}
// Recursively add a observer for each subdirectory
for (File subDir : subDirs) {
addFileAlterationObserver(monitor, listener, subDir);
}
}
示例2: start
import org.apache.commons.io.monitor.FileAlterationListener; //导入依赖的package包/类
public void start() {
String gamePath = Configuration.get().applicationConfiguration().get().getGamePath();
File folder = new File(gamePath + "logs");
this.fileHandler = new MessageFileHandler(gamePath + "logs/Client.txt");
FileAlterationObserver observer = new FileAlterationObserver(folder);
monitor = new FileAlterationMonitor(POLLING_INTERVAL);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
@Override
public void onFileChange(File file) {
fileHandler.parse();
}
};
observer.addListener(listener);
monitor.addObserver(observer);
try {
monitor.start();
} catch (Exception e) {
e.printStackTrace();
}
}
示例3: addMonitor
import org.apache.commons.io.monitor.FileAlterationListener; //导入依赖的package包/类
/**
* 增加监视器
*
* @param path 监听的文件夹路径
* @param listener 事件回调接口
* @return 监听器的id
* @throws JNotifyException {@link JNotifyException}
*/
public static int addMonitor(String path, FileAlterationListener listener) throws JNotifyException {
File file = new File(path);
FileAlterationObserver observer = new FileAlterationObserver(file);
observer.checkAndNotify();
observer.addListener(listener);
int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED;
return JNotify.addWatch(path, mask, false, new FileEventListener(observer));
}
示例4: TorrentFileWatcher
import org.apache.commons.io.monitor.FileAlterationListener; //导入依赖的package包/类
TorrentFileWatcher(final FileAlterationListener listener, final Path monitoredFolder, final Integer interval) {
Preconditions.checkNotNull(listener, "listener cannot be null");
Preconditions.checkNotNull(monitoredFolder, "monitoredFolder cannot be null");
Preconditions.checkArgument(Files.exists(monitoredFolder), "Folder '" + monitoredFolder.toAbsolutePath() + "' does not exists.");
Preconditions.checkNotNull(interval, "interval cannot be null");
Preconditions.checkArgument(interval > 0, "interval cannot be less than 1");
this.listener = listener;
this.monitoredFolder = monitoredFolder.toFile();
this.monitor = new FileAlterationMonitor(interval);
this.observer = new FileAlterationObserver(this.monitoredFolder, TORRENT_FILE_FILTER);
this.observer.addListener(this.listener);
monitor.addObserver(this.observer);
}
示例5: monitorCollection
import org.apache.commons.io.monitor.FileAlterationListener; //导入依赖的package包/类
/**
@method monitorCollection
*/
private FileAlterationMonitor monitorCollection (String collection) throws Exception {
// prtln ("monitorCollection() - " + collection);
AssetCollection assetCollection = this.getCollection(collection);
if (assetCollection == null) {
// Test to see if monitored folder exists
throw new RuntimeException("AssetCollection not found: " + collection);
}
File folder = assetCollection.getDirectory();
if (!folder.exists())
throw new RuntimeException("Folder not found at: " + folder);
FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor =
new FileAlterationMonitor(pollingInterval);
FileAlterationListener listener = new AssetFileListener(collection, this);
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();
// prtln (" .. monitoring " + collection);
return monitor;
}
示例6: getFileObserverListeners
import org.apache.commons.io.monitor.FileAlterationListener; //导入依赖的package包/类
public List<FileObserverListener> getFileObserverListeners() {
List<FileObserverListener> listeners = new ArrayList<FileObserverListener>();
// TODO check if this interface casting is allowed
for (FileAlterationListener listener : observer.getListeners()) {
listeners.add((FileObserverListener) listener);
}
return listeners;
}
示例7: registerDirectoryListener
import org.apache.commons.io.monitor.FileAlterationListener; //导入依赖的package包/类
@Override
@SuppressWarnings("PMD.EmptyCatchBlock")
public synchronized void registerDirectoryListener(File directory, DirectoryListener directoryListener) {
FileAlterationObserver newObserver = new FileAlterationObserver(directory);
FileAlterationObserver oldObserver = directoryObservers.putIfAbsent(directory, newObserver);
final FileAlterationObserver observer;
if (oldObserver == null) {
observer = newObserver;
} else {
observer = oldObserver;
}
SFMF4JFileAlterationListener newListener = new SFMF4JFileAlterationListener(directoryListener);
boolean found = false;
for (FileAlterationListener fal : observer.getListeners()) {
if (newListener.equals(fal)) {
found = true;
break;
}
}
if (!found) {
observer.addListener(newListener);
}
if (observer == newObserver) {
try {
observer.initialize();
fileMonitor.addObserver(observer);
} catch (Exception ex) {
//trap
}
}
}
示例8: start
import org.apache.commons.io.monitor.FileAlterationListener; //导入依赖的package包/类
/**
* コピー元ディレクトリの監視を開始します。
* コピー元ディレクトリ内のファイルが更新されたら、そのファイルをコピー先ディレクトリにコピーします。
*
* @param srcDir コピー元ディレクトリ
* @param dstDir コピー先ディレクトリ
* @return 0 (正常終了)
* @thrrows Exception
*/
public int start(final String srcDir, final String dstDir) throws Exception {
FileAlterationObserver observer = new FileAlterationObserver(srcDir);
int interval = NumberUtils.toInt(System.getProperty(MONITOR_INTERVAL), 1000);
final FileAlterationMonitor monitor
= new FileAlterationMonitor(interval);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
@Override
public void onFileChange(File file) {
if (!file.isFile()) {
return;
}
String relativePath = StringUtils.substringAfter(file.getAbsolutePath(), srcDir);
File dstFile = new File(dstDir, relativePath);
LOG.info("コピーします {} -> {}", file.getAbsolutePath(), dstFile.getAbsolutePath());
try {
FileUtils.copyFile(file, dstFile);
} catch (IOException e) {
LOG.error("ファイルコピーに失敗 ", e);
}
}
};
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();
// TODO ファイル監視方式の統一
String continuFilePath = System.getProperty("cc.cgfile", ".cg");
ControlFile continueFile = new ControlFile(continuFilePath, new ControlFile.Listener() {
@Override
public void onDelete() throws Exception {
monitor.stop();
}
});
continueFile.watch();
while(continueFile.exists()) {
// 制御ファイルが存在し続ける間、処理を継続
}
return 0;
}