zoukankan      html  css  js  c++  java
  • 编程小技巧:自动检测文件修改

    自动检测文件修改


    对于如下情况的文件:
    • 文件较大
    • 计算MD5值很耗时
    • 简单的检测
    原理,很简单:检测最后的修改时间
    劣势:无法检测出文件根本就没有变动的情况(改了,但是内容没有变动)
     
    上代码:
    1. package com.cn.test;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.IOException;
    5. import java.net.URLDecoder;
    6. import java.util.Properties;
    7. publicclassFileTest{
    8. publicstaticvoid main(String[] args)throwsIOException{
    9. Properties prop=newProperties();
    10. String path=null;
    11. System.out.println(newFile("").getCanonicalPath()+"\setting.txt");
    12. // String path = Thread.currentThread().getContextClassLoader().getResource("setting.txt").getPath();
    13. if(path==null)
    14. {
    15. path="./setting.txt";
    16. }
    17. FileInputStream in=null;
    18. File file=null;
    19. long last=0L;
    20. file=newFile(path);
    21. System.out.println("lastmode:"+file.lastModified());
    22. while(true){
    23. // Scanner sc = new Scanner(System.in);
    24. // String s = sc.next();
    25. // char c = s.charAt(0);
    26. if(last==file.lastModified())
    27. {
    28. continue;
    29. }else
    30. {
    31. last=file.lastModified();
    32. }
    33. try{
    34. path =URLDecoder.decode(path,"UTF-8");
    35. in =newFileInputStream(path);
    36. prop.load(in);
    37. prop.list(System.out);
    38. }catch(Exception e){
    39. e.printStackTrace();
    40. }finally{
    41. try{
    42. if(in!=null){
    43. in.close();
    44. }
    45. }catch(IOException e){
    46. e.printStackTrace();
    47. }
    48. }
    49. }
    50. }
    51. }
  • 相关阅读:
    关于虚拟机链接本地磁盘文件的问题
    javaScript学习笔记
    html学习笔记
    eclipse svn插件安装
    python学习笔记一

    hive数据处理
    WordCount实验
    暑假第六周总结
    暑假第五周总结
  • 原文地址:https://www.cnblogs.com/xujintao/p/7587526.html
Copyright © 2011-2022 走看看