技术中心

这里象征着我们的态度和能力

>java 监听文件修改
作者:中国IT实验室    来源:中国IT实验室    发布时间:2012-09-15      浏览次数:8241
分享到:
欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

    在网上找了很多有关java监听文件修改的资料,很多都做得太复杂了;而且用到本地都有问题。这里自己写一个简单的监听文件修改的方法,代码如下:

    [java] view plaincopyprint?

    01.package com.yunho.rule;

    02.

    03.import java.text.SimpleDateFormat;

    04.import java.util.*;

    05.import java.io.File;

    06.import java.io.IOException;

    07.

    08.public class FileListener{

    09.    /**

    10.     * A facility for threads to schedule tasks for future execution in a background thread.

    11.     * Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

    12.     * */

    13.    private Timer timer;

    14.

    15.    /**

    16.     * current time

    17.     * */

    18.    private long currentTime=-1;

    19.

    20.    /**

    21.     * last Modified Time

    22.     * */

    23.    private long lastModifiedTime=-1;

    24.

    25.    /**

    26.     * 分钟

    27.     * */

    28.    private long times=1;

    29.

    30.    /**

    31.     * time

    32.     * */

    33.    private long pollingInterval=1000 * times;

    34.

    35.    /**

    36.     * file path

    37.     * */

    38.    private String filePath="src/com/yunho/rule/rule.xml";

    39.

    40.    public FileListener() {

    41.        File file=new File(filePath);

    42.        lastModifiedTime=file.lastModified();

    43.        currentTime=lastModifiedTime;

    44.    }

    45.

    46.    public static void main(String[] args) {

    47.        FileListener fileListener=new FileListener();

    48.        fileListener.timer = new Timer(true);

    49.        fileListener.start();

    50.    }

    51.

    52.    public void start(){

    53.        timer.schedule(new FileMonitor(), 0, pollingInterval);

    54.         while (true) {

    55.            try {

    56.                int ch = System.in.read();

    57.                System.out.println("ch = "+ch);

    58.                if (ch - c == 0) {

    59.                    System.out.println("QUIT");

    60.                    timer.cancel();

    61.                    break;

    62.                }

    63.            } catch (IOException e) {

    64.                e.printStackTrace();

    65.            }

    66.        }

    67.    }

    68.

    69.    private class FileMonitor extends TimerTask {

    70.        public void run() {

    71.            File file = new File(filePath);

    72.            lastModifiedTime = file.exists() ? file.lastModified() : -1;

    73.            if (currentTime != lastModifiedTime) {

    74.                System.out.println(" File [ " + file.getName() + " ] changed At: "

    75.                        + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")。format(lastModifiedTime));

    76.                currentTime = lastModifiedTime;

    77.            }

    78.        }

    79.    }

    80.}

4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号 增值电信业务经营许可证号:滇B2-20110045
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730