本文整理汇总了Java中java.nio.file.Path.register方法的典型用法代码示例。如果您正苦于以下问题:Java Path.register方法的具体用法?Java Path.register怎么用?Java Path.register使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.Path
的用法示例。
在下文中一共展示了Path.register方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: watchDir
import java.nio.file.Path; //导入方法依赖的package包/类
protected void watchDir(Path dir) throws IOException {
LOG.debug("Registering watch for {}", dir);
if (Thread.currentThread().isInterrupted()) {
LOG.debug("Skipping adding watch since current thread is interrupted.");
}
// check if directory is already watched
// on Windows, check if any parent is already watched
for (Path path = dir; path != null; path = FILE_TREE_WATCHING_SUPPORTED ? path.getParent() : null) {
WatchKey previousWatchKey = watchKeys.get(path);
if (previousWatchKey != null && previousWatchKey.isValid()) {
LOG.debug("Directory {} is already watched and the watch is valid, not adding another one.", path);
return;
}
}
int retryCount = 0;
IOException lastException = null;
while (retryCount++ < 2) {
try {
WatchKey watchKey = dir.register(watchService, WATCH_KINDS, WATCH_MODIFIERS);
watchKeys.put(dir, watchKey);
return;
} catch (IOException e) {
LOG.debug("Exception in registering for watching of " + dir, e);
lastException = e;
if (e instanceof NoSuchFileException) {
LOG.debug("Return silently since directory doesn't exist.");
return;
}
if (e instanceof FileSystemException && e.getMessage() != null && e.getMessage().contains("Bad file descriptor")) {
// retry after getting "Bad file descriptor" exception
LOG.debug("Retrying after 'Bad file descriptor'");
continue;
}
// Windows at least will sometimes throw odd exceptions like java.nio.file.AccessDeniedException
// if the file gets deleted while the watch is being set up.
// So, we just ignore the exception if the dir doesn't exist anymore
if (!Files.exists(dir)) {
// return silently when directory doesn't exist
LOG.debug("Return silently since directory doesn't exist.");
return;
} else {
// no retry
throw e;
}
}
}
LOG.debug("Retry count exceeded, throwing last exception");
throw lastException;
}
示例2: StorageWatcher
import java.nio.file.Path; //导入方法依赖的package包/类
public StorageWatcher ( final StorageManager storageManager, final BaseWatcher baseWatcher, final Path path, final WatchService watcher ) throws IOException
{
this.storageManager = storageManager;
this.baseWatcher = baseWatcher;
this.watcher = watcher;
this.path = path;
this.key = path.register ( watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY );
baseWatcher.addWatcherMap ( path, this );
final File nativeDir = new File ( path.toFile (), "native" );
baseWatcher.addWatcherMap ( nativeDir.toPath (), this );
logger.debug ( "Checking native dir: {}", nativeDir );
if ( nativeDir.exists () && nativeDir.isDirectory () )
{
this.nativeKey = nativeDir.toPath ().register ( this.watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE );
check ();
}
}
示例3: simpleTest
import java.nio.file.Path; //导入方法依赖的package包/类
public void simpleTest(Path path) throws Exception
{
WatchService watchService=FileSystems.getDefault().newWatchService();
path.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
while(true)
{
WatchKey watchKey=watchService.take();
List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
for(WatchEvent<?> event : watchEvents){
//TODO 根据事件类型采取不同的操作。。。。。。。
System.out.println("["+event.context()+"]文件发生了["+event.kind()+"]事件");
}
watchKey.reset();
}
}
示例4: CORSConfigurationFileWatcher
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* Creates a new CORS filter configuration watcher.
*
* @param filterConfig The filter configuration. Must not be
* {@code null}.
*/
public CORSConfigurationFileWatcher(final FilterConfig filterConfig) {
if (filterConfig == null) {
throw new IllegalArgumentException("The servlet filter configuration must not be null");
}
this.filterConfig = filterConfig;
try {
watcher = FileSystems.getDefault().newWatchService();
Path dir = determineConfigDir();
dir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
LOG.fine("CORS Filter: Started watching for configuration file changes within " + dir);
} catch (IOException e) {
LOG.severe("CORS Filter: Failed to initialize file system watcher: " + e.getMessage());
}
}
示例5: fileSystemTraversal
import java.nio.file.Path; //导入方法依赖的package包/类
/**
*
* @param dirsList list of directories on the system
* @param dir path to register directories to be read later
* @param watcher WatchService to register paths
* @param holdValueToKey hashmap containing key,value (paths, project id)
* @throws IOException
*/
private static synchronized void fileSystemTraversal(ArrayList<File> dirsList, Path dir, WatchService watcher, ConcurrentHashMap<String, String> holdValueToKey) throws IOException{
for(int i = 0;i<dirsList.size();i++){
dir = Paths.get(dirsList.get(i).getAbsolutePath()); //get path of selected file
dir.register(watcher,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY); //register directory path with watcher
String subStr = dirsList.get(i).getAbsolutePath();
if(windows){
String let = subStr.substring(0,2);
subStr = let+"\\"+"\\"+subStr.substring(3,subStr.length());
}
Util.debug("WatchService: Registered: "+subStr);
String projID = (String) holdValueToKey.get(subStr); //returns the project id if src folder path is known
if(projID != null){
Util.debug("WatchService: Project ID = "+projID);
String fileAbsPath = fileChosenPaths.get(projID);
String LOCcnt = "0";
//pathLOCcount.put(fileAbsPath, LOCcnt);
//allPathsForFilesWithProjID.put(projID, pathLOCcount); // <ProjID,<AbsoluteFilePath, LOC count>>
}
}
}
示例6: handle
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* Stress the given WatchService, specifically the cancel method, in
* the given directory. Closes the WatchService when done.
*/
static void handle(Path dir, WatchService watcher) {
try {
try {
Path file = dir.resolve("anyfile");
for (int i=0; i<2000; i++) {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
Files.createFile(file);
Files.delete(file);
key.cancel();
}
} finally {
watcher.close();
}
} catch (Exception e) {
e.printStackTrace();
failed = true;
}
}
示例7: addWatch
import java.nio.file.Path; //导入方法依赖的package包/类
@Override
protected WatchKey addWatch(String pathStr) throws IOException {
Path path = Paths.get(pathStr);
try {
WatchKey key = path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
return key;
} catch (ClosedWatchServiceException ex) {
throw new IOException(ex);
}
}
示例8: register
import java.nio.file.Path; //导入方法依赖的package包/类
public void register(FolderResource folder, Path path) {
try {
WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
monitored.put(watchKey, folder);
} catch (IOException e) {
}
}
示例9: observePathSet
import java.nio.file.Path; //导入方法依赖的package包/类
public void observePathSet(PathSet pathSet, Listener listener) throws IOException {
Preconditions.checkNotNull(listener);
Preconditions.checkNotNull(pathSet);
final Path rootPath = pathSet.getRootPath();
log.debug("Watching directory {} for changes matching: {}", rootPath, pathSet);
final WatchKey key = rootPath.register(watcher, new WatchEvent.Kind[]{ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY},
SensitivityWatchEventModifier.HIGH);
// Synchronize on keys to make the two operations atomic.
synchronized (keys) {
keys.putIfAbsent(key, Sets.newConcurrentHashSet());
keys.get(key).add(new WatchPath(pathSet, listener));
}
}
示例10: watch
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* 监听指定路径的修改事件
* @param path
* @return
*/
public static WatchService watch(String path, WatchEvent.Kind kind){
WatchService watchService = null;
try {
watchService = FileSystems.getDefault().newWatchService();
Path dir = FileSystems.getDefault().getPath(path);
dir.register(watchService, kind);
} catch (IOException e) {
log.error("{}监听异常",path,e);
}
return watchService;
}
示例11: registerDirectory
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* Starts watching the directory for changes.
* @param dir Directory to watch.
* @throws IOException If the directory cannot be registered.
*/
private void registerDirectory(final Path dir) throws IOException {
try {
final WatchKey key = dir.register(
this.watcher,
ENTRY_CREATE,
ENTRY_DELETE,
ENTRY_MODIFY
);
this.keys.put(key, dir);
} catch (final IOException ex) {
throw new IOException("Failed to register directory", ex);
}
}
示例12: newRegisterTask
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* Returns a task that updates the registration of a directory with
* a WatchService.
*/
static Callable<Boolean> newRegisterTask(WatchService watcher, Path dir) {
return () -> {
try {
dir.register(watcher, StandardWatchEventKinds.ENTRY_DELETE);
return true;
} catch (ClosedWatchServiceException e) {
return false;
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
};
}
示例13: FileWatcher
import java.nio.file.Path; //导入方法依赖的package包/类
public FileWatcher(final Path watchedDirectory) throws IOException {
this.watchedDirectory = toCanonicalPath(watchedDirectory);
watcher = FileSystems.getDefault().newWatchService();
watchedDirectory.register(watcher, new Kind[] {StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY}, ExtendedWatchEventModifier.FILE_TREE);
thread.setDaemon(true);
thread.start();
}
示例14: manageDirectoryCreation
import java.nio.file.Path; //导入方法依赖的package包/类
private void manageDirectoryCreation(Path filename) throws IOException {
PhotatoFolder newFolder = new PhotatoFolder(rootFolder.fsPath, filename);
PhotatoFolder parentFolder = getCurrentFolder(filename.getParent());
parentFolder.subFolders.put(filename.getFileName().toString(), newFolder);
WatchKey key = filename.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
watchedDirectoriesKeys.put(filename, key);
watchedDirectoriesPaths.put(key, filename);
runInitialFolderExploration(watcher, newFolder);
}
示例15: openAndCloseWatcher
import java.nio.file.Path; //导入方法依赖的package包/类
private static void openAndCloseWatcher(Path dir) {
FileSystem fs = FileSystems.getDefault();
for (int i = 0; i < ITERATIONS_COUNT; i++) {
out.printf("open %d begin%n", i);
try (WatchService watcher = fs.newWatchService()) {
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
} catch (IOException ioe) {
// ignore
} finally {
out.printf("open %d end%n", i);
}
}
}