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

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

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

    隨筆-200  評論-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 無聲 閱讀(379) 評論(0)  編輯  收藏 所屬分類: 職場生活
    主站蜘蛛池模板: www.亚洲精品| 国产午夜无码视频免费网站| 亚洲精品乱码久久久久久久久久久久 | 日韩亚洲人成网站| 国产成人免费a在线视频色戒| 亚洲熟女精品中文字幕| 无码免费午夜福利片在线| 亚洲中文字幕无码mv| 麻豆国产精品入口免费观看| 亚洲精品无码少妇30P| 国产极品粉嫩泬免费观看| 男女交性无遮挡免费视频| 亚洲综合国产精品第一页| 色www永久免费网站| 久久丫精品国产亚洲av不卡| 91大神在线免费观看| 国产精品亚洲精品| 日韩在线免费播放| 一个人看的www视频免费在线观看| 亚洲日本一区二区一本一道| 日韩免费高清播放器| 亚洲国产精品成人精品小说| 亚洲欧洲免费无码| 四虎影视久久久免费观看| 亚洲国产精品无码av| 波多野结衣免费在线| 国产成人亚洲毛片| 国产成人精品日本亚洲网站| 免费成人福利视频| 特级毛片aaaa级毛片免费| 亚洲av无码潮喷在线观看| 97在线线免费观看视频在线观看 | 亚洲欧洲校园自拍都市| 在线看片无码永久免费aⅴ| h视频在线观看免费| 亚洲高清美女一区二区三区| 国产成人啪精品视频免费网| 中文日本免费高清| 亚洲一区二区三区乱码在线欧洲| 免费人成在线观看网站视频| 99爱在线观看免费完整版|