Java listener File Modification

Source: Internet
Author: User
Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://blog.csdn.net/love254443233/article/details/7972525

I have found a lot of information on modifying Java listening files on the Internet. Many of them are too complicated, and there are problems with using them locally. Here, you can write a simple method to modify the listening file. The Code is as follows:

Package COM. yunho. rule; import Java. text. simpledateformat; import Java. util. *; import Java. io. file; import Java. io. ioexception; public class filelistener {/*** a facility for threads to schedule tasks for future execution in a background thread. * tasks may be scheduled for one-time execution, or for repeated execution at regular intervals. **/private timer;/*** current time **/private long cur Required time =-1;/*** last modified time **/private long lastmodifiedtime =-1;/*** minute **/private long times = 1; /*** time **/private long pollinginterval = 1000 * times;/*** file path **/private string filepath = "src/COM/yunho/Rule/rule. XML "; Public filelistener () {file = new file (filepath); lastmodifiedtime = file. lastmodified (); currenttime = lastmodifiedtime;} public static void main (string [] ARGs) {fi Lelistener filelistener = new filelistener (); filelistener. timer = new timer (true); filelistener. start ();} public void start () {timer. schedule (New filemonitor (), 0, pollinginterval); While (true) {try {int CH = system. in. read (); system. out. println ("Ch =" + CH); If (CH-'c' = 0) {system. out. println ("quit"); timer. cancel (); break ;}} catch (ioexception e) {e. printstacktrace () ;}} private class filemonitor exte NDS timertask {public void run () {file = new file (filepath); lastmodifiedtime = file. exists ()? File. lastmodified ():-1; if (currenttime! = Lastmodifiedtime) {system. out. println ("file [" + file. getname () + "] changed at:" + new simpledateformat ("yyyy-mm-dd hh: mm: ss sss "). format (lastmodifiedtime); currenttime = lastmodifiedtime ;}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.