我心飛翔
慢慢的度過
BlogJava
首頁
新隨筆
聯系
聚合
管理
隨筆-200 評論-148 文章-15 trackbacks-0
使用java進行文件夾的拷貝
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)
編輯
收藏
所屬分類:
職場生活
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
kettle ETL java 調用 kettle job 或 transfer
轉win8 64位+Oracle 11g 64位下使用PL/SQL Developer 的解決辦法
Java 獲取當前日期和時間
清除目錄下的SVN信息
Linux下使用gzip壓縮與解壓文件
Linux 下ftp自動登錄
struts1.x防止重復提交
C#正則表達式小結
jbpm4 java.lang.LinkageError: loader constraint violation 包沖突
c#web定時任務
道可道非常道,名可名非常名
<
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
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(5)
給我留言
查看公開留言
查看私人留言
我參與的團隊
JLive開發團隊(0/0)
隨筆分類
(174)
回歸自然(9)
職場生活(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)
收藏夾
共享代碼
開源網站
ajax中國
finereport
hibernate
Java開源大全
java技術論壇
java論壇
java論壇
用戶名parable
linux伊甸園
luanyong
oracle下載
oracle資源
Struts插件
這個插件功能強大,包括JSF,struts,hibernate等
unix論壇
開源力量社區
源程序網站
資料中心
資源網站
parable
朋友博客
東東男
我的博客
最新隨筆
1.?kettle ETL java 調用 kettle job 或 transfer
2.?etl一個例子
3.?論壇
4.?Spring 3.2.4源碼編譯
5.?轉win8 64位+Oracle 11g 64位下使用PL/SQL Developer 的解決辦法
6.?crontab配置詳解
7.?將JSON轉換成MAP的工具類
8.? Java 獲取當前日期和時間
9.?清除目錄下的SVN信息
10.?Symantec 卸載密碼方法
搜索
最新評論
1.?re: 攔截器底層實現原理
政治
--ttt
2.?re: js動態修改表格
454545
--35454
3.?re: ajax局部刷新
jkkhhhl
--34
4.?re: 將JSON轉換成MAP的工具類
v instanceof JSONArray 這段代碼有問題
需要對
List<String>做處理
--丹丹
5.?re: 將JSON轉換成MAP的工具類
評論內容較長,點擊標題查看
--丹丹
閱讀排行榜
1.?OGNL表達式struts2標簽“%,#,$”(78793)
2.?linux 設置系統語言(44867)
3.? Java 獲取當前日期和時間(40395)
4.?Linux下使用gzip壓縮與解壓文件(30903)
5.?將JSON轉換成MAP的工具類(22075)
評論排行榜
1.? RHEL 5 Install Number(16)
2.?ajax局部刷新(8)
3.?RHEL4-U3-i386-AS下載地址(7)
4.?將JSON轉換成MAP的工具類(7)
5.?OGNL表達式struts2標簽“%,#,$”(6)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 無聲
主站蜘蛛池模板:
成人亚洲网站www在线观看
|
久久久久久久亚洲精品
|
免费黄色毛片视频
|
午夜免费福利影院
|
亚洲综合熟女久久久30p
|
亚洲AV无码日韩AV无码导航
|
亚洲网址在线观看
|
久久亚洲精品无码av
|
十八禁无码免费网站
|
日本免费的一级v一片
|
国产亚洲人成网站观看
|
亚洲视频中文字幕在线
|
人体大胆做受免费视频
|
国产精品久久永久免费
|
亚洲人成人无码网www国产
|
日本久久久久亚洲中字幕
|
香蕉视频免费在线
|
国产免费不卡v片在线观看
|
国产亚洲精久久久久久无码77777 国产亚洲精品成人AA片新蒲金
|
精品无码国产污污污免费网站国产
|
亚洲区小说区激情区图片区
|
亚洲国产精品xo在线观看
|
国产精品无码免费专区午夜
|
毛片a级毛片免费观看品善网
|
亚洲无线一二三四区手机
|
亚洲AV电影天堂男人的天堂
|
久久99国产综合精品免费
|
亚洲男人天堂2020
|
国产偷国产偷亚洲清高APP
|
免费三级毛片电影片
|
亚洲国产成人手机在线电影bd
|
精品一区二区三区免费毛片
|
黄页网站免费观看
|
久久久久精品国产亚洲AV无码
|
中文字幕免费播放
|
久久久久久久久亚洲
|
久久午夜羞羞影院免费观看
|
亚洲欧洲日产国码久在线观看
|
野花香在线视频免费观看大全
|
九月丁香婷婷亚洲综合色
|
成人爽a毛片免费
|