引入:将E:java目录下的所有.java文件复制到E:jad目录下,并将原来的文件的扩展名从.java改为.jad
1 package com_2; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.FilenameFilter; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.OutputStream; 10 11 public class Jad2Java { 12 13 public static void main(String[] args) throws Exception { 14 // TODO Auto-generated method stub 15 16 File srcDir = new File("E:\java"); 17 if(!(srcDir.exists() && srcDir.isDirectory())){ 18 throw new Exception("目录不存在"); 19 } 20 File[] files = srcDir.listFiles(new FilenameFilter(){ 21 22 @Override 23 public boolean accept(File dir, String name) { 24 25 return name.endsWith(".java"); 26 } 27 28 }); 29 System.out.println(files.length); 30 File destDir = new File("E:\jad"); 31 if(!destDir.exists()){ 32 destDir.mkdir(); 33 } 34 for(File f : files){ 35 FileInputStream fis = new FileInputStream(f); 36 String destFileName = f.getName().replaceAll("\.java$", ".jad"); 37 FileOutputStream fos = new FileOutputStream(new File(destDir,destFileName)); 38 copy(fis,fos); 39 fis.close(); 40 fos.close(); 41 } 42 43 } 44 45 private static void copy(InputStream ips, OutputStream ops) throws IOException { 46 47 int len = 0; 48 byte[] buf = new byte[1024]; 49 while((len = ips.read(buf)) != -1){ 50 ops.write(buf,0,len); 51 } 52 53 } 54 55 }