zoukankan      html  css  js  c++  java
  • 比较二个文件的最后修改时间FileListener

    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class FileListener {
        public static void main(String[] args) {
            FileListener fileListener = new FileListener();
            fileListener.timer = new Timer(true);
            fileListener.start();
        }
    
        private Timer timer;
    
        private long currentTime = -1;
    
        private long lastModifiedTime = -1;
    
        private long times = 1;
    
        private long pollingInterval = 1000 * times;
    
        private String filePath = "c:\test.txt";
    
        public FileListener() {
            File file = new File(filePath);
            lastModifiedTime = file.lastModified();
            currentTime = lastModifiedTime;
        }
    
        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 (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        private class FileMonitor extends TimerTask {
            public void run() {
                File file = new File(filePath);
                lastModifiedTime = file.exists() ? file.lastModified() : -1;
                if (currentTime != lastModifiedTime) {//1439540671443
                    String string = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")
                            .format(lastModifiedTime);//1439540994156
                    System.out.println("File changed At:" + string);
                    currentTime = lastModifiedTime;
                }
            }
        }
    
    
    }
  • 相关阅读:
    北风网第一季度菜单6
    北风网微信第一季菜单5
    win7卸载打印机驱动
    myeclipse 10激活,本人已测试过可行
    北风网视频菜单4
    Code Project精彩系列(1)
    Code Project精彩系列(1)
    Code Project精彩系列(1)
    实现Windows和Linux之间的文件共享
    实现Windows和Linux之间的文件共享
  • 原文地址:https://www.cnblogs.com/alamps/p/4732748.html
Copyright © 2011-2022 走看看