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

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

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

    隨筆-200  評(píng)論-148  文章-15  trackbacks-0
    I?have?to?say?this?is?a?very?poor?written?OOP?code.?No?wonder?most?people?are?doing?Micrsoft?ASP(because?it?is?not?OOP?at?all!).?Here?is?the?version?I?cooked?for?about?10-15?min?of?my?time.

    public?class?DirCopyUtil?{

    ????public?static?void?copy(File?src,?File?dest)?throws?IOException?{
    ????????getCopyable(src).copy(dest);
    ????}

    ????//this?is?the?factory?pattern.
    ????public?static?Copyable?getCopyable(File?src)?{
    ????????//if?the?source?if?file?then?return?a?file?copier
    ????????if?(src.isFile())?{
    ????????????return?new?FileCopier(src);
    ????????}
    ????????/*else?it?has?to?be?a?directory?copier,?we?could?also?enhance?it?to?be?other?type?of?copier?such?as?httpcopier?etc...*/
    ????????return?new?DirCopier(src);

    ????}

    ????interface?Copyable?{
    ????????void?copy(File?dest)?throws?IOException;
    ????}

    ????static?class?DirCopier?implements?Copyable?{
    ????????File?srcDir;

    ????????public?DirCopier(File?srcDir)?{
    ????????????if?(!srcDir.isDirectory())?{
    ????????????????throw?new?IllegalArgumentException("passed?in?paremter?has?to?be?directory"?+?srcDir);
    ????????????}
    ????????????this.srcDir?=?srcDir;
    ????????}
    ????????/**
    ?????????*?copy?current?directory?content?under?'destRoot'?directory
    ?????????*?@param?destRoot??the?destination?root?directory,?all?the?file?and?sub?directory?will?be?copied?under?this
    ?????????*?destRoot?directory,?if?destRoot?doesn't?exist,?this?will?create?the?directory.
    ?????????*?@throws?IOException
    ?????????*/
    ????????public?void?copy(File?destRoot)?throws?IOException?{
    //??????????1)?if?destRoot?is?not?exist?it?will?create?it
    ????????????if?(!destRoot.exists())?{
    ????????????????if?(!destRoot.mkdirs())?{
    ????????????????????throw?new?IOException("unable?to?create?dir:"?+?destRoot);
    ????????????????}
    ????????????}
    ????????????if?(!destRoot.isDirectory())?{
    ????????????????throw?new?IllegalArgumentException("passed?in?paremter?has?to?be?directory"?+?destRoot);
    ????????????}
    ????????????File[]?dirContents?=?srcDir.listFiles();
    ????????????//we?know?dirContents?can?not?be?null?since?only?file?will?return?null,?so?no?need?to?check?null
    ????????????for?(int?i?=?0;?i?<?dirContents.length;?i++)?{
    ????????????????File?_srcFile?=?dirContents[i];
    ????????????????String?_srcPath?=?_srcFile.getCanonicalPath();
    ????????????????/*now?I?need?to?get?the?relative?path
    ????????????????question?here?is?c:\abc\?cannonicalpath?return??"c:\abc"?or?"c:\\abc\\"??the?answer?is?"c:\\abc"
    ????????????????so?the?following?statement?never?executed,?but?I?am?just?playing?safe?here?since?I?don't?know?if
    ????????????????every?JVM?implementation?will?behave?the?same?as?my?XP?machine?and?Sun?1.4?*/
    ????????????????if?(_srcPath.endsWith(File.separator))?{?//the?endswith?has?subtle?bug,?but?it?should?not?matter
    ????????????????????_srcPath?=?_srcPath.substring(0,?_srcPath.length()?-?File.separator.length());
    ????????????????}
    ????????????????File?dest?=?new?File(destRoot,?_srcPath.substring(_srcPath.lastIndexOf(File.separatorChar)?+?1));
    ????????????????//here?I?wire?back?to?the?factory?method.
    ????????????????getCopyable(dirContents[i]).copy(dest);
    ????????????}
    ????????}

    ????}

    ????static?class?FileCopier?implements?Copyable?{
    ????????File?src;

    ????????public?FileCopier(File?src)?{
    ????????????if?(!src.isFile())?{
    ????????????????throw?new?IllegalArgumentException("passed?in?paremter?has?to?be?file"?+?src);
    ????????????}
    ????????????this.src?=?src;
    ????????}

    ????????/**
    ?????????*?Copy?the?current?file?to?the?dest?file,?if?the?dest?file?doesn't?exist?it?will?be?created.
    ?????????*?@param?dest??the?destination?file
    ?????????*?@throws?IOException
    ?????????*/
    ????????public?void?copy(File?dest)?throws?IOException?{
    ????????????if?(!dest.exists())?{
    ????????????????dest.createNewFile();
    ????????????}
    ????????????if?(!dest.isFile())?{
    ????????????????throw?new?IllegalArgumentException("passed?in?paremter?has?to?be?file"?+?dest);
    ????????????}

    ????????????//do?straight?file?copy
    ????????????FileInputStream?in?=?new?FileInputStream(src);
    ????????????FileOutputStream?out?=?new?FileOutputStream(dest);
    ????????????byte[]?buffer?=?new?byte[1024?*?4];?//4k?buffer
    ????????????int?len?=?0;
    ????????????while?((len?=?in.read(buffer,?0,?buffer.length))?>?0)?{
    ????????????????out.write(buffer,?0,?len);
    ????????????}
    ????????????in.close();
    ????????????out.close();
    ????????}
    ????}
    }
    posted on 2008-07-22 17:01 無(wú)聲 閱讀(384) 評(píng)論(0)  編輯  收藏 所屬分類: 職場(chǎng)生活
    主站蜘蛛池模板: 最近最好的中文字幕2019免费| 亚洲一区二区三区国产精品| 亚洲精品乱码久久久久久中文字幕| 国产精品亚洲一区二区无码| 高清国语自产拍免费视频国产 | 亚洲第一页在线视频| 亚洲伊人久久大香线焦| 国产成人久久精品亚洲小说| 女人被免费视频网站| 亚洲精品美女久久7777777| a毛片免费全部播放完整成| 亚洲日本一区二区三区在线| 中文无码亚洲精品字幕| 国产免费av片在线看| 亚洲中文字幕AV在天堂| 免费毛片在线看不用播放器| 亚洲精品国产精品乱码视色| 久久99热精品免费观看牛牛| 亚洲精品免费在线| 天天看免费高清影视| 在线观看亚洲专区| 中文字幕精品亚洲无线码一区应用| 国产乱妇高清无乱码免费| 亚洲妇熟XXXX妇色黄| 亚洲国产精品免费视频| 亚洲日本久久久午夜精品| 国产成人在线免费观看| 精品免费久久久久国产一区| 久久丫精品国产亚洲av| 成熟女人特级毛片www免费| 免费人成又黄又爽的视频在线电影| 免费观看成人毛片a片2008| 亚洲国产精品久久久久秋霞小| 一本色道久久88综合亚洲精品高清 | 一区二区免费国产在线观看| 国产亚洲视频在线播放| 在线看无码的免费网站| 亚洲va久久久噜噜噜久久天堂| 16女性下面扒开无遮挡免费| 亚洲七久久之综合七久久| 久久亚洲高清综合|