Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342563
  • 博文数量: 329
  • 博客积分: 2633
  • 博客等级: 少校
  • 技术积分: 3633
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-22 15:43
文章分类

全部博文(329)

文章存档

2013年(244)

2012年(46)

2011年(39)

我的朋友

分类: Java

2012-02-27 16:45:53

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStream;
  10. import java.io.OutputStreamWriter;
  11. public class CopyJava {
  12.     public static void main(String[] args) throws IOException {
  13.         copy("e:/src", "e:/desc"); // 这里写好源文件夹和目的文件夹

  14.         System.out.println("ok");
  15.     }
  16.     private static void copy(String srcPath, String descPath)
  17.             throws IOException {
  18.         copy(new File(srcPath), new File(descPath));
  19.     }
  20.     private static void copy(File srcFile, File descFile) throws IOException {
  21.         if (srcFile.isFile()) { // 文件

  22.             File parent = descFile.getParentFile();
  23.             if (!parent.exists()) {
  24.                 parent.mkdirs(); // 创建文件夹

  25.             }
  26.             if (srcFile.getName().endsWith(".java")) {
  27.                 copyJava(srcFile, descFile);
  28.             } else {
  29.                 copyFile(srcFile, descFile);
  30.             }
  31.         } else { // 文件夹

  32.             for (File file : srcFile.listFiles()) {
  33.                 // 相对路径

  34.                 String srcPath = file.getAbsolutePath().substring(
  35.                         srcFile.getAbsolutePath().length());

  36.                 copy(file, new File(descFile.getAbsolutePath() + srcPath));
  37.             }
  38.         }
  39.     }
  40.     private static void copyJava(File srcFile, File descFile)
  41.             throws IOException {

  42.         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
  43.                 new FileOutputStream(descFile)));
  44.         BufferedReader br = new BufferedReader(new InputStreamReader(
  45.                 new FileInputStream(srcFile)));
  46.         String line;
  47.         while ((line = br.readLine()) != null) {
  48.             bw.write(line.replaceFirst(".*/\\*(.*)\\*/", "")); // 注意这里,如果不行,要适当修改

  49.             bw.write("\n");
  50.         }
  51.         br.close();
  52.         bw.close();
  53.     }
  54.     private static void copyFile(File srcFile, File descFile)
  55.             throws IOException {
  56.         OutputStream output = new FileOutputStream(descFile);
  57.         InputStream input = new FileInputStream(srcFile);
  58.         byte[] buffer = new byte[1024 * 4];
  59.         int n = 0;
  60.         while ((n = input.read(buffer)) != -1) {
  61.             output.write(buffer, 0, n);
  62.         }
  63.         input.close();
  64.         output.close();
  65.     }
  66. }
阅读(7266) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~