我心飛翔
慢慢的度過(guò)
BlogJava
首頁(yè)
新隨筆
聯(lián)系
聚合
管理
隨筆-200 評(píng)論-148 文章-15 trackbacks-0
使用java進(jìn)行文件夾的拷貝
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)生活
新用戶注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問(wèn)
管理
相關(guān)文章:
kettle ETL java 調(diào)用 kettle job 或 transfer
轉(zhuǎn)win8 64位+Oracle 11g 64位下使用PL/SQL Developer 的解決辦法
Java 獲取當(dāng)前日期和時(shí)間
清除目錄下的SVN信息
Linux下使用gzip壓縮與解壓文件
Linux 下ftp自動(dòng)登錄
struts1.x防止重復(fù)提交
C#正則表達(dá)式小結(jié)
jbpm4 java.lang.LinkageError: loader constraint violation 包沖突
c#web定時(shí)任務(wù)
道可道非常道,名可名非常名
<
2008年7月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
常用鏈接
我的隨筆
我的評(píng)論
我的參與
最新評(píng)論
留言簿
(5)
給我留言
查看公開留言
查看私人留言
我參與的團(tuán)隊(duì)
JLive開發(fā)團(tuán)隊(duì)(0/0)
隨筆分類
(174)
回歸自然(9)
職場(chǎng)生活(165)
隨筆檔案
(200)
2014年3月 (2)
2014年1月 (1)
2013年9月 (2)
2013年2月 (1)
2013年1月 (2)
2012年8月 (1)
2012年6月 (1)
2012年1月 (1)
2011年12月 (1)
2010年12月 (3)
2010年11月 (10)
2010年10月 (12)
2010年9月 (3)
2010年8月 (5)
2010年7月 (3)
2010年6月 (3)
2010年5月 (1)
2010年4月 (1)
2010年2月 (2)
2010年1月 (2)
2009年11月 (1)
2009年8月 (1)
2009年3月 (3)
2009年2月 (7)
2008年12月 (1)
2008年11月 (1)
2008年9月 (3)
2008年8月 (4)
2008年7月 (4)
2008年6月 (8)
2008年5月 (4)
2008年4月 (4)
2008年2月 (2)
2008年1月 (9)
2007年12月 (11)
2007年11月 (1)
2007年10月 (6)
2007年9月 (1)
2007年8月 (2)
2007年7月 (1)
2007年6月 (1)
2007年5月 (6)
2007年4月 (6)
2006年12月 (9)
2006年11月 (7)
2006年10月 (9)
2006年9月 (5)
2006年7月 (12)
2006年6月 (14)
文章分類
(20)
AJAX(3)
Ant(1)
Delphi(1)
Eclipse(1)
Hibernate(3)
java共享代碼(5)
JBOSS(1)
JSF
linux(4)
Oracle(1)
Spring
sqlserver2000
Struts
Tapestry
webspehre
文章檔案
(15)
2007年6月 (2)
2006年11月 (1)
2006年10月 (5)
2006年6月 (7)
收藏夾
共享代碼
開源網(wǎng)站
ajax中國(guó)
finereport
hibernate
Java開源大全
java技術(shù)論壇
java論壇
java論壇
用戶名parable
linux伊甸園
luanyong
oracle下載
oracle資源
Struts插件
這個(gè)插件功能強(qiáng)大,包括JSF,struts,hibernate等
unix論壇
開源力量社區(qū)
源程序網(wǎng)站
資料中心
資源網(wǎng)站
parable
朋友博客
東東男
我的博客
最新隨筆
1.?kettle ETL java 調(diào)用 kettle job 或 transfer
2.?etl一個(gè)例子
3.?論壇
4.?Spring 3.2.4源碼編譯
5.?轉(zhuǎn)win8 64位+Oracle 11g 64位下使用PL/SQL Developer 的解決辦法
6.?crontab配置詳解
7.?將JSON轉(zhuǎn)換成MAP的工具類
8.? Java 獲取當(dāng)前日期和時(shí)間
9.?清除目錄下的SVN信息
10.?Symantec 卸載密碼方法
搜索
最新評(píng)論
1.?re: 攔截器底層實(shí)現(xiàn)原理
政治
--ttt
2.?re: js動(dòng)態(tài)修改表格
454545
--35454
3.?re: ajax局部刷新
jkkhhhl
--34
4.?re: 將JSON轉(zhuǎn)換成MAP的工具類
v instanceof JSONArray 這段代碼有問(wèn)題
需要對(duì)
List<String>做處理
--丹丹
5.?re: 將JSON轉(zhuǎn)換成MAP的工具類
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--丹丹
閱讀排行榜
1.?OGNL表達(dá)式struts2標(biāo)簽“%,#,$”(78812)
2.?linux 設(shè)置系統(tǒng)語(yǔ)言(44878)
3.? Java 獲取當(dāng)前日期和時(shí)間(40410)
4.?Linux下使用gzip壓縮與解壓文件(30914)
5.?將JSON轉(zhuǎn)換成MAP的工具類(22089)
評(píng)論排行榜
1.? RHEL 5 Install Number(16)
2.?ajax局部刷新(8)
3.?RHEL4-U3-i386-AS下載地址(7)
4.?將JSON轉(zhuǎn)換成MAP的工具類(7)
5.?OGNL表達(dá)式struts2標(biāo)簽“%,#,$”(6)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 無(wú)聲
主站蜘蛛池模板:
最近最好的中文字幕2019免费
|
亚洲一区二区三区国产精品
|
亚洲精品乱码久久久久久中文字幕
|
国产精品亚洲一区二区无码
|
高清国语自产拍免费视频国产
|
亚洲第一页在线视频
|
亚洲伊人久久大香线焦
|
国产成人久久精品亚洲小说
|
女人被免费视频网站
|
亚洲精品美女久久7777777
|
a毛片免费全部播放完整成
|
亚洲日本一区二区三区在线
|
中文无码亚洲精品字幕
|
国产免费av片在线看
|
亚洲中文字幕AV在天堂
|
免费毛片在线看不用播放器
|
亚洲精品国产精品乱码视色
|
久久99热精品免费观看牛牛
|
亚洲精品免费在线
|
天天看免费高清影视
|
在线观看亚洲专区
|
中文字幕精品亚洲无线码一区应用
|
国产乱妇高清无乱码免费
|
亚洲妇熟XXXX妇色黄
|
亚洲国产精品免费视频
|
亚洲日本久久久午夜精品
|
国产成人在线免费观看
|
精品免费久久久久国产一区
|
久久丫精品国产亚洲av
|
成熟女人特级毛片www免费
|
免费人成又黄又爽的视频在线电影
|
免费观看成人毛片a片2008
|
亚洲国产精品久久久久秋霞小
|
一本色道久久88综合亚洲精品高清
|
一区二区免费国产在线观看
|
国产亚洲视频在线播放
|
在线看无码的免费网站
|
亚洲va久久久噜噜噜久久天堂
|
16女性下面扒开无遮挡免费
|
亚洲七久久之综合七久久
|
久久亚洲高清综合
|