All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rcaller.util.FileStatusWatcher Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
/*
 *
 RCaller, A solution for calling R from Java
 Copyright (C) 2010-2015  Mehmet Hakan Satman

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with this program.  If not, see .
 *
 *
 * Mehmet Hakan Satman - [email protected]
 * http://www.mhsatman.com
 * Google code project: https://github.com/jbytecode/rcaller
 * Please visit the blog page with rcaller label:
 * http://stdioe.blogspot.com.tr/search/label/rcaller
 */

package com.github.rcaller.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class FileStatusWatcher {

    public static void waitUntilFileChanged(File file) throws IOException,InterruptedException {
        WatchService watchService = FileSystems.getDefault().newWatchService();
        Path folder = Paths.get(file.getParent());
        folder.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
        while (true) {
            WatchKey key = watchService.take();
            for (WatchEvent watchEvent : key.pollEvents()) {
                final WatchEvent.Kind kind = watchEvent.kind();
                // Entry_modify event
                if (StandardWatchEventKinds.ENTRY_MODIFY == kind) {
                    if(watchEvent.context().toString().equals(file.getName())){
                        watchService.close();
                        return;
                    }
                }
            }
            key.reset();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy