<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 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    一種比較簡陋的方法:

    ?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?

    posted @ 2007-02-08 10:07 lixw 閱讀(1432) | 評論 (2)編輯 收藏


    1?//file?may?be?named?basename_locale.properties
    2?ResourceBundle?bundle?=?ResourceBundle.getBundle("basename");
    3?//?Enumerate?contents?of?resource?bundle
    4?//The?next?two?lines?should?be?in?one?line.
    5?for?(Enumeration?props?=?bundle.getKeys();props.hasMoreElements();?)?{
    6?????String?key?=?(String)props.nextElement();
    7?????process(key,?bundle.getObject(key));
    8?}

    posted @ 2007-02-08 09:43 lixw 閱讀(403) | 評論 (0)編輯 收藏


    1?String?aString?=?"word1?word2?word3";
    2?StringTokenizer?parser?=?new?StringTokenizer(aString);
    3?while?(parser.hasMoreTokens())?{
    4?????processWord(parser.nextToken());
    5?}

    posted @ 2007-02-08 09:43 lixw 閱讀(153) | 評論 (0)編輯 收藏


    ?1?public?static?long?checksum(byte[]?buf)?{
    ?2?????try?{
    ?3?????????CheckedInputStream?cis?=?new?CheckedInputStream(
    ?4????????new?ByteArrayInputStream(buf),new?Adler32());
    ?5?????????byte[]?tempBuf?=?new?byte[128];
    ?6?????????while?(cis.read(tempBuf)?>=?0);????????return?cis.getChecksum().getValue();
    ?7?????}?catch?(IOException?e)?{
    ?8?????????return?-1;
    ?9?????}
    10?}

    posted @ 2007-02-08 09:41 lixw 閱讀(1469) | 評論 (0)編輯 收藏

    ?1?try?{
    ?2??????String?inFilename?=?"infile";
    ?3??????String?outFilename?=?"outfile.zip";
    ?4??????FileInputStream?in?=?new?FileInputStream(inFilename);
    ?5??????ZipOutputStream?out?=?new?ZipOutputStream(new?FileOutputStream(outFilename));
    ?6???????????//?Add?ZIP?entry?to?output?stream.
    ?7??????out.putNextEntry(new?ZipEntry(inFilename));
    ?8??????byte[]?buf?=?new?byte[1024];
    ?9??????int?len;
    10??????while?((len?=?in.read(buf))?>?0)?{
    11??????????out.write(buf,?0,?len);
    12?????}
    13?????????out.closeEntry();
    14?????out.close();
    15?????in.close();
    16?}catch?(IOException?e)?{}



    ?1?try?{
    ?2??????String?inFilename?=?"infile.zip";
    ?3??????String?outFilename?=?"outfile";
    ?4??????ZipInputStream?in?=?new?ZipInputStream(new?FileInputStream(inFilename));
    ?5??????OutputStream?out?=?new?FileOutputStream(outFilename);
    ?6??????ZipEntry?entry;
    ?7??????byte[]?buf?=?new?byte[1024];
    ?8??????int?len;
    ?9??????if?((entry?=?in.getNextEntry())?!=?null)?{
    10??????????while?((len?=?in.read(buf))?>?0)?{
    11???????????????out.write(buf,?0,?len);
    12??????????}
    13??????}
    14??????out.close();
    15??????in.close();
    16??}?catch?(IOException?e)?{
    17??}

    posted @ 2007-02-08 09:35 lixw 閱讀(214) | 評論 (0)編輯 收藏

    僅列出標題
    共4頁: 上一頁 1 2 3 4 下一頁 
    主站蜘蛛池模板: 久久精品国产亚洲AV蜜臀色欲| 亚洲精品在线播放| 亚洲AV成人精品一区二区三区| 无人在线观看完整免费版视频| 亚洲人成影院77777| 很黄很色很刺激的视频免费| 亚洲中文字幕久在线| 免费成人福利视频| 亚洲无吗在线视频| 两个人的视频高清在线观看免费| 丁香婷婷亚洲六月综合色| 歪歪漫画在线观看官网免费阅读 | 日韩免费电影网站| 久久亚洲精品中文字幕三区| 久久久久久成人毛片免费看| 91亚洲自偷手机在线观看| 69精品免费视频| 伊人久久五月丁香综合中文亚洲| 欧洲精品免费一区二区三区| 添bbb免费观看高清视频| 国产亚洲美女精品久久久| 日本不卡免费新一区二区三区| 久久精品亚洲一区二区三区浴池| 麻豆一区二区免费播放网站| 亚洲变态另类一区二区三区| 亚洲国产成人精品91久久久| 精品视频在线免费观看| 亚洲一区中文字幕在线电影网| 浮力影院第一页小视频国产在线观看免费 | 免费观看激色视频网站bd| 亚洲午夜无码久久久久软件| 亚洲成av人在片观看| 无码少妇精品一区二区免费动态| 亚洲不卡中文字幕| 亚洲av日韩av欧v在线天堂| 久久aa毛片免费播放嗯啊| 亚洲欧好州第一的日产suv| 国产gv天堂亚洲国产gv刚刚碰 | 亚洲免费在线播放| 亚洲精品天堂成人片AV在线播放| 在线亚洲人成电影网站色www|