﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-China.JAVA-文章分类-JAVA</title><link>http://www.blogjava.net/kcitwm/category/6060.html</link><description>Prefessional JAVA</description><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 03:05:12 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 03:05:12 GMT</pubDate><ttl>60</ttl><item><title>Java 文件夹考贝类( Java direcotry copy class)</title><link>http://www.blogjava.net/kcitwm/articles/24252.html</link><dc:creator>cl.kcitwm</dc:creator><author>cl.kcitwm</author><pubDate>Fri, 16 Dec 2005 09:03:00 GMT</pubDate><guid>http://www.blogjava.net/kcitwm/articles/24252.html</guid><wfw:comment>http://www.blogjava.net/kcitwm/comments/24252.html</wfw:comment><comments>http://www.blogjava.net/kcitwm/articles/24252.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kcitwm/comments/commentRss/24252.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kcitwm/services/trackbacks/24252.html</trackback:ping><description><![CDATA[<P>package com.kcitwm.util;<BR>/** <BR>&nbsp;* &lt;p&gt;Title: Direcotry copy &lt;/p&gt;<BR>&nbsp;* &lt;p&gt;Description: Direcotry copy&lt;/p&gt;<BR>&nbsp;* &lt;p&gt;Copyright: Copyright (c) 2005&lt;/p&gt;<BR>&nbsp;* &lt;p&gt;Company: cime&lt;/p&gt;<BR>&nbsp;* @author&nbsp;cl.kcitwm<BR>&nbsp;* @version 1.0<BR>&nbsp;* @mail&nbsp;&nbsp; <A href="mailto:kcitwm@gmail.com">kcitwm@gmail.com</A><BR>&nbsp;*/<BR>import java.io.*; <BR>import org.apache.commons.logging.Log;<BR>import org.apache.commons.logging.LogFactory; <BR>&nbsp;</P>
<P>public class FileCopy implements Runnable{<BR>&nbsp;<BR>&nbsp;private static final Log log=LogFactory.getLog(FileCopy.class);<BR>&nbsp;<BR>&nbsp;private String sourceDir;<BR>&nbsp;private String destDir;<BR>&nbsp;private int total=0; <BR>&nbsp;<BR>&nbsp;public void copy(String sourceDir,String destDir){&nbsp;&nbsp;<BR>&nbsp;&nbsp;this.sourceDir=sourceDir;<BR>&nbsp;&nbsp;this.destDir=destDir; &nbsp;&nbsp;<BR>&nbsp;&nbsp;Thread thread=new Thread(this);<BR>&nbsp;&nbsp;thread.start();&nbsp;&nbsp;<BR>&nbsp;}<BR>&nbsp;<BR>&nbsp;private void copyDir(String sourceDir,String destDir) throws Exception{<BR>&nbsp;&nbsp;File sourceFile=new File(sourceDir);<BR>&nbsp;&nbsp;if(!sourceFile.exists())<BR>&nbsp;&nbsp;&nbsp;throw new IOException("source directory is not exists!");<BR>&nbsp;&nbsp;if(sourceFile.isFile()) <BR>&nbsp;&nbsp;&nbsp;throw new IOException("source directory is not directory!");&nbsp; <BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;File destFile=new File(destDir);<BR>&nbsp;&nbsp;if(!destFile.exists())<BR>&nbsp;&nbsp;&nbsp;destFile.mkdir();<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;String tempSource;<BR>&nbsp;&nbsp;String tempDest;<BR>&nbsp;&nbsp;String fileName;<BR>&nbsp;&nbsp;File[] files=sourceFile.listFiles();<BR>&nbsp;&nbsp;for(int i=0;i&lt;files.length;i++){<BR>&nbsp;&nbsp;&nbsp;fileName=files[i].getName();<BR>&nbsp;&nbsp;&nbsp;tempSource=sourceDir+"/"+fileName;<BR>&nbsp;&nbsp;&nbsp;tempDest=destDir+"/"+fileName;<BR>&nbsp;&nbsp;&nbsp;if(files[i].isFile()){ <BR>&nbsp;&nbsp;&nbsp;&nbsp;try{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copyFile(tempSource,tempDest);&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ++total;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception e){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else{<BR>&nbsp;&nbsp;&nbsp;&nbsp;copyDir(tempSource,tempDest);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;} <BR>&nbsp;&nbsp;sourceFile=null;<BR>&nbsp;&nbsp;destFile=null;&nbsp;&nbsp;<BR>&nbsp;} <BR>&nbsp;<BR>&nbsp;private void copyFile(String source,String dest) throws Exception {&nbsp;&nbsp;<BR>&nbsp;&nbsp; File d=new File(dest);<BR>&nbsp;&nbsp; File s=new File(source);<BR>&nbsp;&nbsp; if(d.exists() &amp;&amp; d.lastModified()==s.lastModified())<BR>&nbsp;&nbsp;&nbsp; return;&nbsp; <BR>&nbsp;&nbsp; FileInputStream fis=new FileInputStream(s);<BR>&nbsp;&nbsp; FileOutputStream fos=new FileOutputStream(d);&nbsp; <BR>&nbsp;&nbsp; byte[] buffer=new byte[1024*4];<BR>&nbsp;&nbsp; int n=0;<BR>&nbsp;&nbsp; while((n=fis.read(buffer))!=-1){<BR>&nbsp;&nbsp;&nbsp; fos.write(buffer,0,n);<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; fis.close();<BR>&nbsp;&nbsp; fos.close();<BR>&nbsp;&nbsp; buffer=null;<BR>&nbsp;&nbsp; s=null;<BR>&nbsp;&nbsp; d=null;<BR>&nbsp;}<BR>&nbsp;<BR>&nbsp;public void run(){<BR>&nbsp;&nbsp;try{&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;copyDir(sourceDir,destDir); <BR>&nbsp;&nbsp;&nbsp;log.info(total+" files updated from "+sourceDir+" to "+destDir);<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;catch(Exception e){<BR>&nbsp;&nbsp;&nbsp;log.error(e);<BR>&nbsp;&nbsp;} <BR>&nbsp;}<BR>&nbsp;<BR>}</P><img src ="http://www.blogjava.net/kcitwm/aggbug/24252.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kcitwm/" target="_blank">cl.kcitwm</a> 2005-12-16 17:03 <a href="http://www.blogjava.net/kcitwm/articles/24252.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>