/** * Write a program to copy all the. java files in the D:\java directory to the D:\jad directory and change the extension of the original file from. java to. jad.
* *: The Listfiles method accepts a FileFilter object, the FileFilter object is the policy object of the filter, and different people provide different filefilter implementations, that is, different filtering strategies are provided.
* * */package test;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import Java.io.FilenameFilter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream; public class test{public static void Main (String [] args) throws exception{File srcdir = new File ("Java");//directory name if ( ! (Srcdir.exists () &&srcdir.isdirectory ())) {throw new Exception ("directory does not exist.
"); }//directory *.java file[] files = srcdir.listfiles (new FilenameFilter () {//Name filter public boolean accept (File dir,s
Tring name) {return Name.endswith (". Java");
}
});
System.out.println (files.length); number of//*.java file Destdir = new file ("Jad"); if (!destdir.exists ()) destDir.mkdir ()///Create folder Jad for (File f:files) {FileInputStream FIS = new FileInputStream (f);//input byte stream//*.java to *.jad---- -------regular expression. Need ' \. ', and \ need to write as \ \.
$ is the regular expression terminator String destFileName = F.getname (). ReplaceAll ("\\.java$", ". Jad");
Output byte stream fileoutputstream fos = new FileOutputStream (new File (Destdir,destfilename));
Copy (Fis,fos);
Fis.close ();
Fos.close ();
}//Byte stream copy private static void copy (InputStream IPs, OutputStream Ops) throws exception{int len = 0;
byte[] buf = new byte[1024];//The maximum number of bytes fetched from the input stream each time 1024 while (len = Ips.read (buf))!=-1) {ops.write (Buf,0,len);
}
}
}