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) {//
String string = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")
.format(lastModifiedTime);//
System.out.println("File changed At:" + string);
currentTime = lastModifiedTime;
}
}
} }

比较二个文件的最后修改时间FileListener的更多相关文章

  1. java获得文件的最后修改时间

    原文:http://www.open-open.com/code/view/1453190044980 java的File类的lastModified()方法可以返回文件的最后修改时间: String ...

  2. delphi 获取文件的最新修改时间 http://www.delphitop.com/html/wenjian/64.html

    delphi 获取文件的最新修改时间 作者:admin 来源:未知 日期:2010/1/28 13:15:22 人气:1054 标签: QQ空间新浪微博腾讯微博腾讯朋友QQ收藏百度空间百度贴吧更多0 ...

  3. delphi获取文件的创建/修改时间、按时间删除指定文件下的文件

    uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrl ...

  4. mp4、AAC数据格式、解析文件的创建修改时间

    AAC文件 资料: http://blog.csdn.net/u013427969/article/details/53091594 http://blog.chinaunix.net/uid-260 ...

  5. Centos-修改文件访问和修改时间-touch

    touch 如果文件存在,则改变文件的访问时间和修改时间,如果不存在则创建一个空文件 相关选项 -a   更改文件访问时间为当前系统时间 -m     更改文件修改时间为当前系统时间 -c   如果文 ...

  6. java实现遍历文件目录,根据文件最后的修改时间排序,并将文件全路径存入List集合

    package com.ultra.aliyun.control.main; import java.io.File; import java.util.ArrayList; import java. ...

  7. Shell中取得文件的最后修改时间

    stat -c %y file 取得修改日期 -,-,-

  8. 使用PowerShell来修改文件访问,创建,修改时间属性

    Function Set-FileTimeStamps { Param ( [Parameter(mandatory=$true)] [string[]]$path, [datetime]$date ...

  9. 定时删除文件夹"$1"下最后修改时间大于当前时间"$2"天的文件

    shell 脚本: #!/bin/bash now=`date "+%Y-%m-%d_%H:%M:%S"`      #获取当前时间 echo "当前时间: " ...

随机推荐

  1. Bootstrap 巨幕页头缩略图和警告框组件

    一.巨幕组件 //在固定的范围内,有圆角 <div class="container"> <div class="jumbotron"> ...

  2. 1014 C语言文法

    <程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> ...

  3. C++ 字符串操作常见函数

    //字符串拷贝,排除指定字符 char *strcpy_exclude_char(char *dst, const int dst_len, const char *src, const char * ...

  4. Mac版 MicrosoftOffice2015 办公软件 破解教程

    来自:http://bbs.feng.com/read-htm-tid-9704285.html 下载链接:http://pan.baidu.com/s/1dD6lBFz  提取密码:xu5n 补丁: ...

  5. Estimating Project Costs

    The Wideman Comparative Glossary of Common Project Management Terms describes estimating cost as, &q ...

  6. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  7. Java: Difference between ArrayList and LinkedList

    Basically, they are just two different implementations of List interface. LinkedList is implemented ...

  8. saltstack之(十二)配置管理mount

    线上很多服务器都需要挂载存储上的公共目录,并实现开机启动(/etc/fstab),比如web的静态文件共享目录,日志远程集中收集等. 一.批量挂载部分. 1.在node1上配置nfs服务器,有关nfs ...

  9. svn使用svnsync实现双机热备

    前提条件: 主:10.11.100.205从:10.11.100.50 源目录:http://10.11.100.205/svn/rep-ops目标目录:http://10.11.100.50/svn ...

  10. Chrome控制台调试工具用法

    下面我们来看看console里面具体提供了哪些方法可以供我们平时调试时使用. Console API 当打开 firebug (也包括 Chrome 等浏览器的自带调试工具),window 下面会注册 ...