隨筆-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 無聲 閱讀(384) 評論(0)  編輯  收藏 所屬分類: 職場生活
          道可道非常道,名可名非常名
          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(5)

          我參與的團隊

          隨筆分類(174)

          隨筆檔案(200)

          文章分類(20)

          文章檔案(15)

          收藏夾

          開源網站

          朋友博客

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 云林县| 麻江县| 达日县| 青铜峡市| 鹿邑县| 龙口市| 株洲市| 长泰县| 玉溪市| 包头市| 廉江市| 会泽县| 玉龙| 武安市| 仪征市| 肥西县| 含山县| 涡阳县| 灌云县| 广饶县| 横峰县| 石台县| 武乡县| 交城县| 托克逊县| 黔南| 庄浪县| 中牟县| 石狮市| 南召县| 漯河市| 上蔡县| 霍邱县| 禄丰县| 三明市| 宾阳县| 乌兰浩特市| 绥滨县| 开封县| 普兰店市| 高唐县|