import java.io.File; /** * User: Administrator * Date: 13-8-29 下午2:15 * 复制原文件夹下的所有文件到指定文件夹 */ public class DirCopy { private static final String URL1 = "D:/PractiseFile/io"; //原文件夹 private static final String URL2 = "D:/PractiseFile/ionew"; //目标文件夹 public static void main(String args[]) { dirCopy(URL1, URL2); } private static void dirCopy(String url1, String url2){ File sourceDir = new File(url1); if (sourceDir.exists()) { if (sourceDir.isDirectory()) { File targetDir = new File(url2); if (!targetDir.exists()) { targetDir.mkdir(); } File[] dirList = sourceDir.listFiles(); if (dirList != null) { for (File s : dirList) { dirCopy(s.getAbsolutePath(), url2 + File.separator + s.getName()); } } } else { fileCopy(url1, url2); } } else { System.out.println("source dir not found"); } } private static void fileCopy(String url1, String url2){ try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(url1), "GBK")); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(url2), "GBK")); String s; while ((s = reader.readLine()) != null) { writer.write(s); writer.newLine(); } writer.flush(); writer.close(); reader.close(); } catch (FileNotFoundException e) { System.out.println("file not found"); } catch (IOException e) { System.out.println("system error"); } } }