Chinaunix首页 | 论坛 | 博客
  • 博客访问: 107562
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 352
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-23 12:37
文章分类

全部博文(41)

文章存档

2015年(1)

2014年(28)

2013年(12)

我的朋友

分类: Java

2014-08-31 09:37:49

编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并 将原来文件的扩展名从.java改为.jad

点击(此处)折叠或打开

  1. package com.wp;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.FilenameFilter;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. public class Java2Jad {
  10.     
  11.     public static void main(String[] args) throws Exception {
  12.                      File srcDir =new File("d:\\java");
  13.                             
  14.                     if(!(srcDir.exists()&& srcDir.isDirectory()))
  15.                         throw new Exception("The directory is not exist!!");
  16.                     File[] files= srcDir.listFiles(
  17.                             new FilenameFilter(){
  18.                                 public boolean accept(File dir, String name) {
  19.                                     return name.endsWith(".java"); 
  20.                                 }
  21.                             }
  22.                     );
  23.                     
  24.                     System.out.println(files.length);
  25.                     
  26.                     File destDir= new File("d:\\jad");
  27.                     if(!destDir.exists())
  28.                         destDir.mkdir();
  29.                     
  30.                     for(File f:files){
  31.                         FileInputStream fis = new FileInputStream(f);
  32.                         String destFileName = f.getName().replaceAll("\\.java$", ".jad");
  33.                         FileOutputStream fos = new FileOutputStream(new
  34.                                 File(destDir,destFileName));
  35.                         copy(fis,fos);
  36.                         fis.close();
  37.                         fos.close();
  38.                     }
  39.     }
  40.     
  41.     private static void copy(InputStream ips,OutputStream ops) throws Exception{
  42.                     int len = 0;
  43.                     byte[] buf =new byte[1024];
  44.                     while((len =ips.read(buf)) != -1){
  45.                         ops.write(buf,0,len);
  46.                     }
  47.     }
  48. }

阅读(1027) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~