<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在线观看| 久久久久久久亚洲精品| 免费黄色毛片视频| 午夜免费福利影院| 亚洲综合熟女久久久30p| 亚洲AV无码日韩AV无码导航| 亚洲网址在线观看| 久久亚洲精品无码av| 十八禁无码免费网站| 日本免费的一级v一片| 国产亚洲人成网站观看| 亚洲视频中文字幕在线| 人体大胆做受免费视频| 国产精品久久永久免费| 亚洲人成人无码网www国产| 日本久久久久亚洲中字幕| 香蕉视频免费在线| 国产免费不卡v片在线观看| 国产亚洲精久久久久久无码77777 国产亚洲精品成人AA片新蒲金 | 精品无码国产污污污免费网站国产 | 亚洲区小说区激情区图片区| 亚洲国产精品xo在线观看| 国产精品无码免费专区午夜| 毛片a级毛片免费观看品善网| 亚洲无线一二三四区手机| 亚洲AV电影天堂男人的天堂| 久久99国产综合精品免费| 亚洲男人天堂2020| 国产偷国产偷亚洲清高APP| 免费三级毛片电影片| 亚洲国产成人手机在线电影bd | 精品一区二区三区免费毛片| 黄页网站免费观看| 久久久久精品国产亚洲AV无码| 中文字幕免费播放| 久久久久久久久亚洲| 久久午夜羞羞影院免费观看| 亚洲欧洲日产国码久在线观看| 野花香在线视频免费观看大全| 九月丁香婷婷亚洲综合色| 成人爽a毛片免费|