Java basic IO file copy exercises

Source: Internet
Author: User

/**
* Write a program that copies all of the. java files in the specified directory to another directory, and when the copy succeeds, change the suffix. java to. txt.
*/

1. We can see the problem or use the idea of small to do
Analysis: 1. Copy >>> Choose what stream?? >>> copy I wrote in the previous blog, we use the word stream, character stream read Only write
2.java End >> We can think of the EndsWith () method in string.
3. Copy successful and then change the suffix name >>file a method of Renameto ()
4. What if a file with the same name in the folder of the copied file is received?? Think about this.

We look at the code: the first step: 1. Copy >>> Choose what stream?? >>> copy I wrote in the previous blog, we use the word stream, character stream read Only write
2.java End >> We can think of the EndsWith () method in string.

/** Copy of File*/    Private Static voidCopy (file src, file dest)throwsIOException {file[] files=Src.listfiles (); //determine if the array is empty        if(Files! =NULL) {            //iterating through an array             for(File file:files) {//Files that are files and end in Java                if(File.isfile () && file.getname (). EndsWith (". Java")) {                    //Fast buffer stream copy speedBufferedinputstream bis =NewBufferedinputstream (Newfileinputstream (file)); Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (NewFile (dest, File.getname ())); byte[] by =New byte[1024]; intLen = 0;  while(len = Bis.read (by))! =-1) {Bos.write (by,0, Len); }                    //off-StreamBos.close ();                    Bis.close (); //if the file}Else if(File.isdirectory ()) {//in determining whether this folder is empty or without Java files, we don't copy the definition remove ()                    if(remove (file)) {file NewFile=NewFile (dest, File.getname ());                        Newfile.mkdirs ();                    Copy (file, newfile); }                }            }        }    }    Private Static BooleanRemove (file file) {file[] files=File.listfiles (); //determine if the array is empty        if(File! =NULL) {             for(File file2:files) {//is a file and the file ending in Java returns true                if(File2.isfile () && file2.getname (). EndsWith (". Java")) {                    return true; //This is the key, not the file, but also the file to contain the Java file}Else if(File2.isdirectory () &&Remove (file2)) {                    return true; }            }        }        //condition does not match return false        return false; }}

The second step: 3. Copy successful and then change suffix name >>file a method of Renameto () with a suffix name
4. What if a file with the same name in the folder of the copied file is received?? Think about this.

    //. Java modified to. txt    Private Static voidRevamp (File dest)throwsioexception{file[] File=Dest.listfiles ();  for(File file2:file) {if(File2.isfile ()) {//Gets the file name stringString SB =File2.getname (); //determine if it is a Java file                if(Sb.endswith (". Java") {StringBuilder sb1=NewStringBuilder (SB); //Replace the Java string with a. txt stringSb1.replace (Sb1.tostring (). IndexOf (". Java"), Sb1.length (), ". txt"); //New FileFile NewFile =NewFile (dest, sb1.tostring ()); //one more time. Loop through the files and folders in this number of times Group                     for(File file3:file) {//If you have the same file in it, delete it in the rename                        if(File3.isfile () &&newfile.getname (). Equals (File3.getname ()))                        {File3.delete (); }                    }                    //determine if there is a duplicate name in the folderFile2.renameto (NewFile); }                //if it's a file, it's recursive .}Else if(File2.isdirectory ()) {revamp (file2); }        }    }

Main method

 Public Static void throws IOException {        // package file address        new file ("E:\\io stream");         New File ("E:\\io");         // Copy         Copy (SRC, dest);         //  Renaming         revamp (dest);    }

Java basic IO file copy exercises

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.