自动检测文件修改
对于如下情况的文件:
- 文件较大
- 计算MD5值很耗时
- 简单的检测
原理,很简单:检测最后的修改时间
劣势:无法检测出文件根本就没有变动的情况(改了,但是内容没有变动)
上代码:
package com.cn.test;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.net.URLDecoder;import java.util.Properties;publicclassFileTest{publicstaticvoid main(String[] args)throwsIOException{Properties prop=newProperties();String path=null;System.out.println(newFile("").getCanonicalPath()+"\setting.txt");// String path = Thread.currentThread().getContextClassLoader().getResource("setting.txt").getPath();if(path==null){path="./setting.txt";}FileInputStream in=null;File file=null;long last=0L;file=newFile(path);System.out.println("lastmode:"+file.lastModified());while(true){// Scanner sc = new Scanner(System.in);// String s = sc.next();// char c = s.charAt(0);if(last==file.lastModified()){continue;}else{last=file.lastModified();}try{path =URLDecoder.decode(path,"UTF-8");in =newFileInputStream(path);prop.load(in);prop.list(System.out);}catch(Exception e){e.printStackTrace();}finally{try{if(in!=null){in.close();}}catch(IOException e){e.printStackTrace();}}}}}