<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Thinking

    快樂編程,開心生活
    posts - 21, comments - 27, trackbacks - 0, articles - -5
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    多線程監聽文件改動

    Posted on 2007-02-08 10:07 lixw 閱讀(1432) 評論(2)  編輯  收藏
    一種比較簡陋的方法:

    ?1?ActionListener?taskPerformer?=?new?ActionListener()?{
    ?2?????????????public?void?actionPerformed(ActionEvent?evt)?{
    ?3?????????????????log.info("monitor?is?running?at?"?+?new?java.util.Date());
    ?4?????????????????String?configfile?=?(String)getServletContext().getAttribute("configfile");
    ?5?????????????????if(configfile?!=?null?&&?configfile.length()!=0){
    ?6?????????????????????try{
    ?7?????????????????????????File?file?=?new?File(configfile);
    ?8?????????????????????????if(file.lastModified()?>?lastModifyTime){
    ?9?????????????????????????????lastModifyTime?=?file.lastModified();
    10?????????????????????????????loadProp();
    11?????????????????????????}
    12?????????????????????}catch(Exception?e){
    13?????????????????????????log.error("construct?file:"?+?configfile?+?"?exception");
    14?????????????????????????e.printStackTrace();
    15?????????????????????}
    16?????????????????}
    17?????????????}
    18? };
    19?????????
    20? //啟動監聽線程
    21?new?Timer(delay,?taskPerformer).start();


    來自geosoft.no的解決方法:
    ?1?import?java.io.File;
    ?2?
    ?3?/**
    ?4??*?Interface?for?listening?to?disk?file?changes.
    ?5??*?@see?FileMonitor
    ?6??*?
    ?7??*?@author?<a?href="mailto:jacob.dreyer@geosoft.no">Jacob?Dreyer</a>
    ?8??*/???
    ?9?public?interface?FileListener
    10?{
    11???/**
    12????*?Called?when?one?of?the?monitored?files?are?created,?deleted
    13????*?or?modified.
    14????*?
    15????*?@param?file??File?which?has?been?changed.
    16????*/
    17???void?fileChanged?(File?file);
    18?}

    ??1?import?java.util.*;
    ??2?import?java.io.File;
    ??3?import?java.lang.ref.WeakReference;
    ??4?
    ??5?/**
    ??6??*?Class?for?monitoring?changes?in?disk?files.
    ??7??*?Usage:
    ??8??*
    ??9??*????1.?Implement?the?FileListener?interface.
    ?10??*????2.?Create?a?FileMonitor?instance.
    ?11??*????3.?Add?the?file(s)/directory(ies)?to?listen?for.
    ?12??*
    ?13??*?fileChanged()?will?be?called?when?a?monitored?file?is?created,
    ?14??*?deleted?or?its?modified?time?changes.
    ?15??*
    ?16??*?@author?<a?href="mailto:jacob.dreyer@geosoft.no">Jacob?Dreyer</a>
    ?17??*/???
    ?18?public?class?FileMonitor
    ?19?{
    ?20???private?Timer???????timer_;
    ?21???private?HashMap?????files_;???????//?File?->?Long
    ?22???private?Collection??listeners_;???//?of?WeakReference(FileListener)
    ?23????
    ?24?
    ?25???/**
    ?26????*?Create?a?file?monitor?instance?with?specified?polling?interval.
    ?27????*?
    ?28????*?@param?pollingInterval??Polling?interval?in?milli?seconds.
    ?29????*/
    ?30???public?FileMonitor?(long?pollingInterval)
    ?31???{
    ?32?????files_?????=?new?HashMap();
    ?33?????listeners_?=?new?ArrayList();
    ?34?
    ?35?????timer_?=?new?Timer?(true);
    ?36?????timer_.schedule?(new?FileMonitorNotifier(),?0,?pollingInterval);
    ?37???}
    ?38?
    ?39?
    ?40???
    ?41???/**
    ?42????*?Stop?the?file?monitor?polling.
    ?43????*/
    ?44???public?void?stop()
    ?45???{
    ?46?????timer_.cancel();
    ?47???}
    ?48???
    ?49?
    ?50???/**
    ?51????*?Add?file?to?listen?for.?File?may?be?any?java.io.File?(including?a
    ?52????*?directory)?and?may?well?be?a?non-existing?file?in?the?case?where?the
    ?53????*?creating?of?the?file?is?to?be?trepped.
    ?54????*?<p>
    ?55????*?More?than?one?file?can?be?listened?for.?When?the?specified?file?is
    ?56????*?created,?modified?or?deleted,?listeners?are?notified.
    ?57????*?
    ?58????*?@param?file??File?to?listen?for.
    ?59????*/
    ?60???public?void?addFile?(File?file)
    ?61???{
    ?62?????if?(!files_.containsKey?(file))?{
    ?63???????long?modifiedTime?=?file.exists()???file.lastModified()?:?-1;
    ?64???????files_.put?(file,?new?Long?(modifiedTime));
    ?65?????}
    ?66???}
    ?67?
    ?68???
    ?69?
    ?70???/**
    ?71????*?Remove?specified?file?for?listening.
    ?72????*?
    ?73????*?@param?file??File?to?remove.
    ?74????*/
    ?75???public?void?removeFile?(File?file)
    ?76???{
    ?77?????files_.remove?(file);
    ?78???}
    ?79?
    ?80?
    ?81???
    ?82???/**
    ?83????*?Add?listener?to?this?file?monitor.
    ?84????*?
    ?85????*?@param?fileListener??Listener?to?add.
    ?86????*/
    ?87???public?void?addListener?(FileListener?fileListener)
    ?88???{
    ?89?????//?Don't?add?if?its?already?there
    ?90?????for?(Iterator?i?=?listeners_.iterator();?i.hasNext();?)?{
    ?91???????WeakReference?reference?=?(WeakReference)?i.next();
    ?92???????FileListener?listener?=?(FileListener)?reference.get();
    ?93???????if?(listener?==?fileListener)
    ?94?????????return;
    ?95?????}
    ?96?
    ?97?????//?Use?WeakReference?to?avoid?memory?leak?if?this?becomes?the
    ?98?????//?sole?reference?to?the?object.
    ?99?????listeners_.add?(new?WeakReference?(fileListener));
    100???}
    101?
    102?
    103???
    104???/**
    105????*?Remove?listener?from?this?file?monitor.
    106????*?
    107????*?@param?fileListener??Listener?to?remove.
    108????*/
    109???public?void?removeListener?(FileListener?fileListener)
    110???{
    111?????for?(Iterator?i?=?listeners_.iterator();?i.hasNext();?)?{
    112???????WeakReference?reference?=?(WeakReference)?i.next();
    113???????FileListener?listener?=?(FileListener)?reference.get();
    114???????if?(listener?==?fileListener)?{
    115?????????i.remove();
    116?????????break;
    117???????}
    118?????}
    119???}
    120?
    121?
    122???
    123???/**
    124????*?This?is?the?timer?thread?which?is?executed?every?n?milliseconds
    125????*?according?to?the?setting?of?the?file?monitor.?It?investigates?the
    126????*?file?in?question?and?notify?listeners?if?changed.
    127????*/
    128???private?class?FileMonitorNotifier?extends?TimerTask
    129???{
    130?????public?void?run()
    131?????{
    132???????//?Loop?over?the?registered?files?and?see?which?have?changed.
    133???????//?Use?a?copy?of?the?list?in?case?listener?wants?to?alter?the
    134???????//?list?within?its?fileChanged?method.
    135???????Collection?files?=?new?ArrayList?(files_.keySet());
    136???????
    137???????for?(Iterator?i?=?files.iterator();?i.hasNext();?)?{
    138?????????File?file?=?(File)?i.next();
    139?????????long?lastModifiedTime?=?((Long)?files_.get?(file)).longValue();
    140?????????long?newModifiedTime??=?file.exists()???file.lastModified()?:?-1;
    141?
    142?????????//?Chek?if?file?has?changed
    143?????????if?(newModifiedTime?!=?lastModifiedTime)?{
    144?
    145???????????//?Register?new?modified?time
    146???????????files_.put?(file,?new?Long?(newModifiedTime));
    147?
    148???????????//?Notify?listeners
    149???????????for?(Iterator?j?=?listeners_.iterator();?j.hasNext();?)?{
    150?????????????WeakReference?reference?=?(WeakReference)?j.next();
    151?????????????FileListener?listener?=?(FileListener)?reference.get();
    152?
    153?????????????//?Remove?from?list?if?the?back-end?object?has?been?GC'd
    154?????????????if?(listener?==?null)
    155???????????????j.remove();
    156?????????????else
    157???????????????listener.fileChanged?(file);
    158???????????}
    159?????????}
    160???????}
    161?????}
    162???}
    163?
    164?
    165???/**
    166????*?Test?this?class.
    167????*?
    168????*?@param?args??Not?used.
    169????*/
    170???public?static?void?main?(String?args[])
    171???{
    172?????//?Create?the?monitor
    173?????FileMonitor?monitor?=?new?FileMonitor?(1000);
    174?
    175?????//?Add?some?files?to?listen?for
    176?????monitor.addFile?(new?File?("D:\\myjava\\JCreatorWorkspace\\FileMonitor\\test.txt"));
    177?
    178?????//?Add?a?dummy?listener
    179?????monitor.addListener?(monitor.new?TestListener());
    180?
    181?????//?Avoid?program?exit
    182?????while?(!false)?;
    183???}
    184?
    185???
    186???private?class?TestListener
    187?????implements?FileListener
    188???{
    189?????public?void?fileChanged?(File?file)
    190?????{
    191???????System.out.println?("File?["?+?file.getName()?+?"]?changed?At:"?+?new?java.util.Date());
    192?????}
    193???}
    194?}
    195?
    196?
    197?


    評論

    # re: 多線程監聽文件改動  回復  更多評論   

    2007-02-08 13:50 by azure
    不錯,很實用。

    # re: 多線程監聽文件改動  回復  更多評論   

    2007-03-27 11:03 by elary
    有沒有辦法做個監聽程序放到服務器里自動運行?

    只有注冊用戶登錄后才能發表評論。


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 国产亚洲成在线播放va| 亚洲午夜电影在线观看高清 | 亚洲一区二区三区首页| 国产精品黄页免费高清在线观看| 免费久久精品国产片香蕉| 男人的天堂av亚洲一区2区| 午夜一级免费视频| 国产成人亚洲精品无码AV大片| 国产在线播放免费| 无码AV动漫精品一区二区免费| 亚洲XX00视频| 国产在线观看免费av站| 国产亚洲真人做受在线观看| 中文字幕av免费专区| 亚洲AV无一区二区三区久久| 四虎影视在线永久免费观看| 精品亚洲成A人在线观看青青| 免费看一级做a爰片久久| 一区二区视频免费观看| 国产AV无码专区亚洲Av| 一级毛片免费播放| 亚洲Av高清一区二区三区| 国产精品色午夜免费视频| av片在线观看永久免费| 亚洲av无码一区二区三区不卡| 久久久久高潮毛片免费全部播放| 亚洲理论片在线中文字幕| 免费无码一区二区三区蜜桃大| 无码 免费 国产在线观看91| 亚洲A∨无码无在线观看| 操美女视频免费网站| 2022免费国产精品福利在线 | 午夜国产精品免费观看| 亚洲第一成年网站视频 | www.黄色免费网站| 一本久久免费视频| 亚洲最新视频在线观看| 免费看又爽又黄禁片视频1000| 四虎成人精品国产永久免费无码| 亚洲人成电影福利在线播放| 在线观看免费宅男视频|