java - Comparing current date time(live) with file last modified -


i want program output such that, whenever receives text file client, print "a new file has been received!". text files store in c:/users/%userprofile%/desktop/ad.

the code below check whether new text file has been received directory.

import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import java.text.dateformat; import java.text.simpledateformat; import java.util.calendar; import java.util.date;  import javax.swing.timer;   public class test {      final dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss");     static int count = 0;     static file[] f = null;     static date date = new date();       static calendar = null;      public static void main(string[] args) {         dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss");         while(true) {             try {                 thread.sleep(2000);             } catch (interruptedexception e1) {                 e1.printstacktrace();             }             f = new file("c:/users/roberts/desktop/ad").listfiles();             int l = f.length;             system.out.println("there " +  l +  " files in directory");              (int = 0; < l; i++) {                 system.out.println("file " + +  " date modified " + dateformat.format(f[i].lastmodified()));             }         }     } } 

output of total number of files in directory = 1 , last modified

enter image description here

output of total number of files in directory after file received = 2 , last modified

enter image description here

now in order print "a new file has been received!" need compare current date time last modified date time of file.

this did

import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import java.text.dateformat; import java.text.simpledateformat; import java.util.calendar; import java.util.date;  import javax.swing.timer;   public class test {      final dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss");     int interval = 1000;     static int count = 0;     static file[] f = null;     static date date = new date();       static calendar = null;      public static void main(string[] args) {         dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss");         while(true) {                  f = new file("c:/users/roberts/desktop/ad").listfiles();                 int l = f.length;                 system.out.println(l);                 new timer(1000, new actionlistener() {                     @override                     public void actionperformed(actionevent e) {                         = calendar.getinstance();                         (int = 0; < l; i++) {                             if(dateformat.format(now.gettime()).equals(dateformat.format(f[i].lastmodified()))) {                                 system.out.println("a new file has been received!");                             }                         }                      }                 }).start();              try {                 thread.currentthread().join();             } catch (interruptedexception e1) {                 // todo auto-generated catch block                 e1.printstacktrace();             }         }     } } 

i tried doesn't print "a new file has been received!". please help

watching file system changes best done using watch service. right polling file changes, instead watch service uses native windows events.

first need create instance of watchservice class.

path path = paths.get("c:\mydir"); watchservice service = path.getfilesystem().newwatchservice(); 

next have decide kind of events want monitor. think want informed when new files appear in folder.

path.register(service, standardwatcheventkinds.entry_create); 

next, service acts buffer of events. suggest put processing of buffer in seperate thread.

// repeat forever (;;) {   // block until there's inside buffer   watchkey key = service.take();    // 1 watchkey can contain multiple events, need iterate these.   (watchevent<?> event : key.pollevents())   {     //todo: handle events   } } 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -