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.

Source: Internet
Author: User
/** * 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);

 }
		
	}
	
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.