集册 Java实例教程 监视图像文件夹

监视图像文件夹

欢马劈雪     最近更新时间:2020-01-02 10:19:05

516
注意图像文件夹

import java.io.IOException;

import java.nio.file.FileSystems;

import java.nio.file.Files;

import java.nio.file.Path;
/*来自 
 NowJava.com*/

import java.nio.file.Paths;

import java.nio.file.StandardWatchEventKinds;

import java.nio.file.WatchEvent;

import java.nio.file.WatchEvent.Kind;

import java.nio.file.WatchKey;

import java.nio.file.WatchService;

import java.text.SimpleDateFormat;

import java.util.Date;/*来自 时   代    Java - nowjava.com*/

import java.util.concurrent.TimeUnit;


public class Main {

  public static void main(String[] args) throws Exception {

    final Path path = Paths.get("C:/security");

    WatchService watchService = FileSystems.getDefault().newWatchService();

    path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);

    OUTERMOST: while (true) {

      WatchKey key = watchService.poll(11, TimeUnit.SECONDS);

      if (key == null) {

        System.out.println("canceled!");

        break;

      } else {

        for (WatchEvent<?> watchEvent : key.pollEvents()) {

          Kind<?> kind = watchEvent.kind();

          if (kind == StandardWatchEventKinds.OVERFLOW) {

            continue;

          }

          if (kind == StandardWatchEventKinds.ENTRY_CREATE) {

            WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;

            Path filename = watchEventPath.context();

            Path child = path.resolve(filename);

            if (Files.probeContentType(child).equals("image/jpeg")) {

              SimpleDateFormat dateFormat = new SimpleDateFormat(

                  "yyyy-MMM-dd HH:mm:ss");

              System.out.println("event: " + dateForm
展开阅读全文