A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 向天宣战 中级黑马   /  2015-6-11 12:04  /  136 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /**
  2. * 编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
  3. *
  4. * @author zyy
  5. *
  6. */
  7. public class Test9 {

  8.         public static void main(String[] args) {
  9.                 copy(new File("D:\\WorkSpace\\java\\exam\\src\\com\\itheima"),
  10.                                 new File("D:\\demo"));
  11.         }

  12.         /**
  13.          * 文件所有拷贝
  14.          *
  15.          * @param source
  16.          *            数据源
  17.          * @param target
  18.          *            目标源
  19.          */
  20.         private static void copy(File source, File target) {
  21.                 if (!source.exists()) {
  22.                         throw new RuntimeException("source file is not exit");
  23.                 }
  24.                 // 判断文件夹是否存在
  25.                 if (!target.exists()) {
  26.                         target.mkdirs();// 创建多及目录
  27.                 }
  28.                 File[] file = source.listFiles();
  29.                 for (File f : file) {
  30.                         // 获取文件名称
  31.                         String fileName = f.getName();
  32.                         if (null != fileName && fileName.endsWith(".java")) {
  33.                                 copyFile(
  34.                                                 f,
  35.                                                 new File(target + File.separator
  36.                                                                 + fileName.replace(".java", ".txt")));
  37.                         }

  38.                 }
  39.         }

  40.         /**
  41.          * 文件单独拷贝
  42.          *
  43.          * @param source
  44.          *            数据源
  45.          * @param target
  46.          *            目标源
  47.          */
  48.         private static void copyFile(File source, File target) {
  49.                 //获取文件输入,输出流
  50.                 FileInputStream fis = null;
  51.                 FileOutputStream fos = null;
  52.                 try {
  53.                         fis = new FileInputStream(source);
  54.                         fos = new FileOutputStream(target);
  55.                         int length = 0;
  56.                         byte[] buf = new byte[1024];
  57.                         while ((length = fis.read(buf)) != -1) {
  58.                                 fos.write(buf, 0, length);
  59.                         }
  60.                 } catch (IOException e) {
  61.                         e.printStackTrace();
  62.                         throw new RuntimeException("文件复制失败");
  63.                 } finally {
  64.                         // 关闭流
  65.                         try {
  66.                                 if (fos != null)
  67.                                         fos.close();
  68.                         } catch (IOException e) {
  69.                                 throw new RuntimeException("写出文件关闭失败");
  70.                         }

  71.                         try {
  72.                                 if (fis != null)
  73.                                         fis.close();
  74.                         } catch (IOException e) {
  75.                                 throw new RuntimeException("读取文件关闭失败");
  76.                         }
  77.                 }
  78.         }

  79. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马