Write a program that copies all the. java files in the D: \ java directory to the D: \ Jad directory and changes the original file's extension from. java to. jad

Source: Internet
Author: User
Tags file copy

1. Write a program to copy all the. java files from the D: \ java directory to the D: \ Jad directory and change the original file extension from. java to. jad

Package Copy;import Java.io.bufferedinputstream;import java.io.bufferedoutputstream;import java.io.File;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.filenamefilter;import java.io.IOException; Import Java.io.inputstream;import Java.io.OutputStream; Public classCopytest { Public Static voidMain (string[] args) throws Exception {file file=NewFile ("D:\\java"); //determine if a directory exists            if(! (File.exists () &&file.isdirectory ())) {                Throw NewException ("directory does not exist"); }            //locate the. java file under the folderFile[] Files=file.listfiles (NewFilenameFilter () { PublicBoolean Accept (File dir, String name) {//TODO auto-generated Method Stub                    returnName.endswith (". Java");            }            }); System. out. println (Files.length); //Copying Files//Locate the target directoryFile targetfile=NewFile ("D:\\jad"); //determines whether the directory exists, does not exist, creates            if(! (Targetfile.exists () &&targetfile.isdirectory ()))            {Targetfile.mkdirs (); }            //Replace file name             for(File f:files) {//1. Using file streams for File replicationString targetfilename=f.getname (). Replace (". Java",". Jad");//FileInputStream in=new FileInputStream (f);//FileOutputStream out=new FileOutputStream (New File (Targetfile,targetfilename));//copy (in,out);//2. Use buffered streams to improve read and write efficiencyBufferedinputstream bis=NewBufferedinputstream (NewFileInputStream (f)); Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (NewFile (targetfile,targetfilename));            CopyDemo2 (Bis,bos); }                    }                      /** * Use file stream for file copy * @author Smy * @throws Exception*/         Public Static voidCopy (InputStreaminch, OutputStream out) throws exception{byte[] data =New byte[1024x768*Ten]; intLen =-1;  while(len =inch. Read (data))!=-1){                 out. write (data,0, Len); }                            inch. Close ();  out. Close (); }                   /** * use buffered streams for faster read/write efficiency * * Buffered streams are a pair of advanced streams that can be used to improve read-write efficiency. * Advanced Streaming: Advanced Streaming is used to process other streams, and advanced streams cannot exist independently, because there is no meaning, the purpose of using advanced streams is to solve some of the complexity of reading and writing operations.         Simplify our * Read and write operations.         * Low-level flow: The data source is clear, the real responsibility for reading and writing data flow.         * Read and write operations must require a low-level stream, but do not necessarily use * advanced streaming. * @author Smy * @throws Exception*/         Public Static voidCopyDemo2 (InputStream bis,outputstream Bos) throws exception{intD =-1; /** Buffer Stream internally maintains an array of bytes as a buffer.             When we call the Read () method reading A * Byte, the buffer stream reads a number of bytes at a time * and buffers it, then returns the first byte, when we call the Read method again to read a byte,             * It will immediately return the second byte in the buffer, * it will not happen again until all the bytes are returned * The actual read operation, reading a set of bytes into the buffer zone.             * So in essence, by reading a few bytes at a time, * reduce the read efficiency of increased reading times. */             while((D=bis.read ())!=-1) {bos.write (d);            } bis.close ();            Bos.close (); }        }

Write a program that copies all the. java files in the D: \ java directory to the D: \ Jad directory and changes the original file's extension from. java to. jad

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.