1 <div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-j"><li class="alt"><span><span><pre name=</span><span class="string">"code"</span><span> </span><span class="keyword">class</span><span>=</span><span class="string">"java"</span><span>></span><span class="keyword">package</span><span> com.czp; </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> java.io.File; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.io.FileInputStream; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> java.io.FileOutputStream; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.util.Scanner; </span></span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> CopyFolder { </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">// 允许复制的文件类型</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> String[] filterFile = { </span><span class="string">".java"</span><span>, </span><span class="string">".xml"</span><span>, </span><span class="string">".xdl"</span><span>, </span></span></li><li class="alt"><span> <span class="string">".properties"</span><span>, </span><span class="string">".sql"</span><span>, </span><span class="string">".jupiter"</span><span>, </span><span class="string">".wsdl"</span><span> }; </span></span></li><li class=""><span> <span class="keyword">private</span><span> </span><span class="keyword">long</span><span> total = 0l; </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> main(String[] args) </span><span class="keyword">throws</span><span> Exception { </span></span></li><li class="alt"><span> </span></li><li class=""><span> Scanner scanner = <span class="keyword">new</span><span> Scanner(System.in); </span></span></li><li class="alt"><span> String answer = <span class="keyword">null</span><span>; </span></span></li><li class=""><span> <span class="keyword">do</span><span> { </span></span></li><li class="alt"><span> System.out.println(<span class="string">"Enter src folder:"</span><span>); </span></span></li><li class=""><span> String srcStr = scanner.next(); </span></li><li class="alt"><span> String destStr; </span></li><li class=""><span> System.out.println(<span class="string">"Now enter dest folder:"</span><span>); </span></span></li><li class="alt"><span> destStr = scanner.next(); </span></li><li class=""><span> File src = <span class="keyword">new</span><span> File(srcStr); </span></span></li><li class="alt"><span> File des = <span class="keyword">new</span><span> File(destStr); </span></span></li><li class=""><span> <span class="keyword">new</span><span> CopyFolder().copyFolder(src, des, filterFile); </span></span></li><li class="alt"><span> System.out.println(<span class="string">"Continue ?y or n"</span><span>); </span></span></li><li class=""><span> answer = scanner.next(); </span></li><li class="alt"><span> } <span class="keyword">while</span><span> (answer.equalsIgnoreCase(</span><span class="string">"Y"</span><span>)); </span></span></li><li class=""><span> scanner.close(); </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">/** * * @param folder * @param filterFile * @throws Exception */</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> copyFolder(File srcFolder, File destFolder, String[] filterFile) </span></span></li><li class="alt"><span> <span class="keyword">throws</span><span> Exception { </span></span></li><li class=""><span> File[] files = srcFolder.listFiles(); </span></li><li class="alt"><span> <span class="keyword">for</span><span> (File file : files) { </span></span></li><li class=""><span> <span class="keyword">if</span><span> (file.isFile()) { </span></span></li><li class="alt"><span> String pathname = destFolder + File.separator + file.getName(); </span></li><li class=""><span> <span class="keyword">for</span><span> (String suff : filterFile) { </span></span></li><li class="alt"><span> <span class="keyword">if</span><span> (pathname.endsWith(suff)) { </span></span></li><li class=""><span> File dest = <span class="keyword">new</span><span> File(pathname); </span></span></li><li class="alt"><span> File destPar = dest.getParentFile(); </span></li><li class=""><span> destPar.mkdirs(); </span></li><li class="alt"><span> <span class="keyword">if</span><span> (!dest.exists()) { </span></span></li><li class=""><span> dest.createNewFile(); </span></li><li class="alt"><span> } </span></li><li class=""><span> copyFile(file, dest); </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span> } <span class="keyword">else</span><span> { </span></span></li><li class=""><span> copyFolder(file, destFolder, filterFile); </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">/***</span> </span></li><li class=""><span><span class="comment"> * * copy file * * @param src * @param dest * @param status * @throws</span> </span></li><li class="alt"><span><span class="comment"> * IOException</span> </span></li><li class=""><span><span class="comment"> */</span><span> </span></span></li><li class="alt"><span> <span class="keyword">private</span><span> </span><span class="keyword">void</span><span> copyFile(File src, File dest) </span><span class="keyword">throws</span><span> Exception { </span></span></li><li class=""><span> FileInputStream input = <span class="keyword">null</span><span>; </span></span></li><li class="alt"><span> FileOutputStream outstrem = <span class="keyword">null</span><span>; </span></span></li><li class=""><span> <span class="keyword">try</span><span> { </span></span></li><li class="alt"><span> input = <span class="keyword">new</span><span> FileInputStream(src); </span></span></li><li class=""><span> outstrem = <span class="keyword">new</span><span> FileOutputStream(dest); </span></span></li><li class="alt"><span> outstrem.getChannel().transferFrom(input.getChannel(), <span class="number">0</span><span>,input.available()); </span></span></li><li class=""><span> total++; </span></li><li class="alt"><span> String temp =String.format(<span class="string">"\ncopy:%s size:%s to: %s complate: %s"</span><span>, src,src.length(),dest,total); </span></span></li><li class=""><span> System.out.print(temp); </span></li><li class="alt"><span> } <span class="keyword">catch</span><span> (Exception e) { </span></span></li><li class=""><span> <span class="keyword">throw</span><span> e; </span></span></li><li class="alt"><span> } <span class="keyword">finally</span><span> { </span></span></li><li class=""><span> outstrem.flush(); </span></li><li class="alt"><span> outstrem.close(); </span></li><li class=""><span> input.close(); </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span>} </span></li><li class=""><span></pre><br> </span></li><li class="alt"><span><br> </span></li><li class=""><span><pre></pre> </span></li><li class="alt"><span><pre></pre> </span></li></ol></div><pre name="code" class="java" style="display: none;"><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">package</span><span> com.czp; </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> java.io.File; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.io.FileInputStream; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> java.io.FileOutputStream; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.util.Scanner; </span></span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> CopyFolder { </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">// 允许复制的文件类型</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> String[] filterFile = { </span><span class="string">".java"</span><span>, </span><span class="string">".xml"</span><span>, </span><span class="string">".xdl"</span><span>, </span></span></li><li class="alt"><span> <span class="string">".properties"</span><span>, </span><span class="string">".sql"</span><span>, </span><span class="string">".jupiter"</span><span>, </span><span class="string">".wsdl"</span><span> }; </span></span></li><li class=""><span> <span class="keyword">private</span><span> </span><span class="keyword">long</span><span> total = 0l; </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> main(String[] args) </span><span class="keyword">throws</span><span> Exception { </span></span></li><li class="alt"><span> </span></li><li class=""><span> Scanner scanner = <span class="keyword">new</span><span> Scanner(System.in); </span></span></li><li class="alt"><span> String answer = <span class="keyword">null</span><span>; </span></span></li><li class=""><span> <span class="keyword">do</span><span> { </span></span></li><li class="alt"><span> System.out.println(<span class="string">"Enter src folder:"</span><span>); </span></span></li><li class=""><span> String srcStr = scanner.next(); </span></li><li class="alt"><span> String destStr; </span></li><li class=""><span> System.out.println(<span class="string">"Now enter dest folder:"</span><span>); </span></span></li><li class="alt"><span> destStr = scanner.next(); </span></li><li class=""><span> File src = <span class="keyword">new</span><span> File(srcStr); </span></span></li><li class="alt"><span> File des = <span class="keyword">new</span><span> File(destStr); </span></span></li><li class=""><span> <span class="keyword">new</span><span> CopyFolder().copyFolder(src, des, filterFile); </span></span></li><li class="alt"><span> System.out.println(<span class="string">"Continue ?y or n"</span><span>); </span></span></li><li class=""><span> answer = scanner.next(); </span></li><li class="alt"><span> } <span class="keyword">while</span><span> (answer.equalsIgnoreCase(</span><span class="string">"Y"</span><span>)); </span></span></li><li class=""><span> scanner.close(); </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">/** * * @param folder * @param filterFile * @throws Exception */</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> copyFolder(File srcFolder, File destFolder, String[] filterFile) </span></span></li><li class="alt"><span> <span class="keyword">throws</span><span> Exception { </span></span></li><li class=""><span> File[] files = srcFolder.listFiles(); </span></li><li class="alt"><span> <span class="keyword">for</span><span> (File file : files) { </span></span></li><li class=""><span> <span class="keyword">if</span><span> (file.isFile()) { </span></span></li><li class="alt"><span> String pathname = destFolder + File.separator + file.getName(); </span></li><li class=""><span> <span class="keyword">for</span><span> (String suff : filterFile) { </span></span></li><li class="alt"><span> <span class="keyword">if</span><span> (pathname.endsWith(suff)) { </span></span></li><li class=""><span> File dest = <span class="keyword">new</span><span> File(pathname); </span></span></li><li class="alt"><span> File destPar = dest.getParentFile(); </span></li><li class=""><span> destPar.mkdirs(); </span></li><li class="alt"><span> <span class="keyword">if</span><span> (!dest.exists()) { </span></span></li><li class=""><span> dest.createNewFile(); </span></li><li class="alt"><span> } </span></li><li class=""><span> copyFile(file, dest); </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span> } <span class="keyword">else</span><span> { </span></span></li><li class=""><span> copyFolder(file, destFolder, filterFile); </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">/***</span> </span></li><li class=""><span><span class="comment"> * * copy file * * @param src * @param dest * @param status * @throws</span> </span></li><li class="alt"><span><span class="comment"> * IOException</span> </span></li><li class=""><span><span class="comment"> */</span><span> </span></span></li><li class="alt"><span> <span class="keyword">private</span><span> </span><span class="keyword">void</span><span> copyFile(File src, File dest) </span><span class="keyword">throws</span><span> Exception { </span></span></li><li class=""><span> FileInputStream input = <span class="keyword">null</span><span>; </span></span></li><li class="alt"><span> FileOutputStream outstrem = <span class="keyword">null</span><span>; </span></span></li><li class=""><span> <span class="keyword">try</span><span> { </span></span></li><li class="alt"><span> input = <span class="keyword">new</span><span> FileInputStream(src); </span></span></li><li class=""><span> outstrem = <span class="keyword">new</span><span> FileOutputStream(dest); </span></span></li><li class="alt"><span> outstrem.getChannel().transferFrom(input.getChannel(), <span class="number">0</span><span>,input.available()); </span></span></li><li class=""><span> total++; </span></li><li class="alt"><span> String temp =String.format(<span class="string">"\ncopy:%s size:%s to: %s complate: %s"</span><span>, src,src.length(),dest,total); </span></span></li><li class=""><span> System.out.print(temp); </span></li><li class="alt"><span> } <span class="keyword">catch</span><span> (Exception e) { </span></span></li><li class=""><span> <span class="keyword">throw</span><span> e; </span></span></li><li class="alt"><span> } <span class="keyword">finally</span><span> { </span></span></li><li class=""><span> outstrem.flush(); </span></li><li class="alt"><span> outstrem.close(); </span></li><li class=""><span> input.close(); </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span>} </span></li></ol></div><pre name="code" class="java" style="display: none;">package com.czp; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.util.Scanner; 7 8 9 public class CopyFolder { 10 11 // 允许复制的文件类型 12 public static String[] filterFile = { ".java", ".xml", ".xdl", 13 ".properties", ".sql", ".jupiter", ".wsdl" }; 14 private long total = 0l; 15 16 public static void main(String[] args) throws Exception { 17 18 Scanner scanner = new Scanner(System.in); 19 String answer = null; 20 do { 21 System.out.println("Enter src folder:"); 22 String srcStr = scanner.next(); 23 String destStr; 24 System.out.println("Now enter dest folder:"); 25 destStr = scanner.next(); 26 File src = new File(srcStr); 27 File des = new File(destStr); 28 new CopyFolder().copyFolder(src, des, filterFile); 29 System.out.println("Continue ?y or n"); 30 answer = scanner.next(); 31 } while (answer.equalsIgnoreCase("Y")); 32 scanner.close(); 33 } 34 35 /** * * @param folder * @param filterFile * @throws Exception */ 36 public void copyFolder(File srcFolder, File destFolder, String[] filterFile) 37 throws Exception { 38 File[] files = srcFolder.listFiles(); 39 for (File file : files) { 40 if (file.isFile()) { 41 String pathname = destFolder + File.separator + file.getName(); 42 for (String suff : filterFile) { 43 if (pathname.endsWith(suff)) { 44 File dest = new File(pathname); 45 File destPar = dest.getParentFile(); 46 destPar.mkdirs(); 47 if (!dest.exists()) { 48 dest.createNewFile(); 49 } 50 copyFile(file, dest); 51 } 52 } 53 } else { 54 copyFolder(file, destFolder, filterFile); 55 } 56 } 57 } 58 59 /*** 60 * * copy file * * @param src * @param dest * @param status * @throws 61 * IOException 62 */ 63 private void copyFile(File src, File dest) throws Exception { 64 FileInputStream input = null; 65 FileOutputStream outstrem = null; 66 try { 67 input = new FileInputStream(src); 68 outstrem = new FileOutputStream(dest); 69 outstrem.getChannel().transferFrom(input.getChannel(), 0,input.available()); 70 total++; 71 String temp =String.format("\ncopy:%s size:%s to: %s complate: %s", src,src.length(),dest,total); 72 System.out.print(temp); 73 } catch (Exception e) { 74 throw e; 75 } finally { 76 outstrem.flush(); 77 outstrem.close(); 78 input.close(); 79 } 80 } 81 } 82 </pre><br> 83 <br> 84 <pre></pre> 85 <pre></pre> 86 87 </pre>