Some actions about folders

Source: Internet
Author: User

ImportJava.io.*; Public classFileoperate {//Create the specified directory    voidmakedirectory (String dirName) {File file=NewFile (DirName);//instantiating a File object        Try{            if(File.exists ()) {System.out.println (DirName+ "already exists"); }            Else{file.mkdir (); System.out.println (DirName+ "was created!"); }        }Catch(Exception e) {e.printstacktrace (); }    }            //empties all files in the specified directory    voidemptydirectory (String dirName) {File file=NewFile (DirName);//instantiating a File object        if(File.isdirectory ())//If the file object is a folder{File f[]= File.listfiles ();//Remove all files from this folder                         for(inti=0;i<f.length;i++) {f[i].delete (); }        }        Else{//Delete a file if it is not a folderFile.delete (); }                                        }        //Lists all file recursion under the specified directory    voidListall (String dirName) {File file=NewFile (DirName);//instantiating a File object        if(File.isdirectory ())//If the file object is an object{File f[]= File.listfiles ();//Remove all files from this folder            if(f!=NULL)            {                 for(inti=0;i<f.length;i++) {//to process each element of a file arrayListall (F[i].tostring ());//recursively process the file                }            }        }Else                                    //output file information directly if it is not a folderSystem.out.println (file); }    //get all filenames named under a suffix in the specified directory ·     voidGettypepart (String dirname,string flag) {File file=NewFile (DirName);//instantiating a File object        if(File.isdirectory ())//If the file object is an object{File f[]= File.listfiles ();//Remove all files from this folder            if(f!=NULL)            {                 for(inti=0;i<f.length;i++) {//to process each element of a file arrayListall (F[i].tostring ());//recursively process the file                }            }        }        Else if(File.getname (). EndsWith (flag)) {//output file information directly if it is not a folderSystem.out.println (file); }                                        }            //strline.indexof ("Hello", id)! =-1//fuzzy search for specified files under specified directory    voidsearchfile (String dirname,string key) {File file=NewFile (DirName);//instantiating a File object        if(File.isdirectory ())//If the file object is an object{File f[]= File.listfiles ();//Remove all files from this folder            if(f!=NULL)            {                 for(inti=0;i<f.length;i++) {//to process each element of a file arrayListall (F[i].tostring ());//recursively process the file                }            }        }        Else if(File.getname (). IndexOf (key,0)!=-1) {//output file information directly if it is not a folderSystem.out.println (file); }                                }         Public Static voidMain (string[] args) {//TODO auto-generated Method Stub            }}

Some actions about folders

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.