Guest User

java.nio.files.StandardWatchEventKinds.ENTRY_MODIFY problem

a guest
Sep 14th, 2011
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. import java.io.Writer;
  2. import java.nio.charset.Charset;
  3. import java.nio.file.ClosedWatchServiceException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.StandardWatchEventKinds;
  7. import java.nio.file.WatchEvent;
  8. import java.nio.file.WatchKey;
  9. import java.nio.file.WatchService;
  10. import java.nio.file.attribute.FileAttribute;
  11.  
  12. class NioModifiedProblem {
  13.     public static void println(String str) {
  14.         System.out.println(str);
  15.     }
  16.     public static void printFileInfo(Path path) {
  17.         try {
  18.             println(String.format("File %s, size %d, modified %s", path, Files.size(path), Files.getLastModifiedTime(path)));
  19.         } catch (Throwable e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.     public static void main(String[] args) {
  24.         String data = "Some not too long string goes here. Goes. Goes.";
  25.         try {
  26.             final Path path = Files.createTempFile("nioProblem", ".tmp", new FileAttribute[0]);
  27.             path.toFile().deleteOnExit();
  28.             println("Created");
  29.             printFileInfo(path);
  30.        
  31.             Thread thread = new Thread() {
  32.                 public void run() {
  33.                     try {
  34.                         final Path parent = path.getParent();
  35.                         final WatchService service = parent.getFileSystem().newWatchService();
  36.                         WatchKey key = parent.register(service, StandardWatchEventKinds.ENTRY_MODIFY);
  37.                         try {
  38.                             while (true) {
  39.                                 for (WatchEvent<?> event : service.take().pollEvents()){
  40.                                     Path modifiedPath = parent.resolve((Path)event.context());
  41.                                     println("Path "+modifiedPath+" modified EVENT."); // This is printed only once, on file opening.
  42.                                     printFileInfo(modifiedPath);
  43.                                 }
  44.                             }
  45.                         } catch (ClosedWatchServiceException e) {
  46.                             println("Service closed");
  47.                         }
  48.                     } catch (Throwable e) {
  49.                         e.printStackTrace();
  50.                     } finally {
  51.                         println("Watcher thread exiting");
  52.                     }
  53.                 }
  54.             };
  55.             thread.setDaemon(true);
  56.             thread.start();
  57.  
  58.             Thread.sleep(1000);
  59.             Writer fw = Files.newBufferedWriter(path, Charset.forName("UTF-8"));
  60.             println("Opened");
  61.             printFileInfo(path);
  62.  
  63.             Thread.sleep(1000);
  64.             fw.write(data);
  65.             println("Written");
  66.             printFileInfo(path);
  67.            
  68.             fw.close();
  69.             println("Closed");
  70.             printFileInfo(path);
  71.            
  72.             Thread.sleep(1000);
  73.             println("Sleeped");
  74.             printFileInfo(path);
  75.             return;
  76.         } catch (Throwable e) {
  77.             e.printStackTrace();
  78.         }
  79.     }
  80. }
  81.  
  82. // Java(TM) SE Runtime Environment (build 1.7.0-b147)
  83. // Output:
  84. // Created
  85. // File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 0, modified 2011-09-14T16:20:06.782Z
  86. // Opened
  87. // Path C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp modified EVENT.
  88. // File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 0, modified 2011-09-14T16:20:07.807Z
  89. // File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 0, modified 2011-09-14T16:20:07.807Z
  90. // Written
  91. // File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 0, modified 2011-09-14T16:20:07.807Z
  92. // Closed
  93. // File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 47, modified 2011-09-14T16:20:08.81Z
  94. // Sleeped
  95. // File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 47, modified 2011-09-14T16:20:08.81Z
Add Comment
Please, Sign In to add comment