Comprehensive application of File

Source: Internet
Author: User

 Packagecom.beiwo.homework;ImportJava.io.File;/*4. Look up all the files in a folder and analyze them in a certain format: 1. A file exists in a folder, and subfolders 2. In addition to the current folder, files in subfolders also need to be found.                          3. Sub-file Find files and the current file to find files is the same operation, you can define a method to do file lookup 3. Delete data from a week ago 2. Gets the size of all the files in the folder 1. Determine if there are hidden files */ Public classHomework {Static inttotalsize = 0;//will only be created once        /**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//1. Get the specified folderFile File =NewFile ("D:\\java courseware and materials \ \ 18th Class"); //2. Methods for invoking lookupsGetFile (file, 1); //3. Total size of the printed fileSYSTEM.OUT.PRINTLN ("Total size of the folder:" +totalsize*1.0/(1024x768 *1024*1024) + "G"); }        //4. Judging hidden files     Public Static Booleangethiddenfile (file file) {returnFile.ishidden (); }        //3. Get the size of all files in a folder     Public Static voidgetfilesize (file file) {if(File.isfile ()) {totalsize+=file.length (); }             }            //delete a week ago data     Public Static voiddeleteafterweekfile (file file) {//1. Get the last time the file was manipulated        LongLasttime =file.lastmodified (); //2. Get the current time in milliseconds        LongCurrentTime =System.currenttimemillis (); //3. How to do time difference milliseconds        LongTime = CurrentTime-Lasttime; //4. Convert the number of milliseconds to days        intDay = (int) (Time/(1000*60*60*24)); if(day>=7){                        //Delete file Operation            if(File.isfile ()) {file.delete (); }                    }                    }            //Differentiate directory structure     Public StaticString Flag (intcount) {String str="";  for(inti = 0; I < count; i++) {str+="--"+" "; }        returnstr; }        /*** Find all the files in the folder *@paramTargetPath: The path of the file that needs to be found *@paramcount: Record the number of subdirectories*/     Public Static voidGetFile (File file,intCount) {//1//gets the abstract path of the file//File File = new file (TargetPath);                if(File.exists ()) {//represents a folder exists//get files and folders in the current directoryfile[] Files =File.listfiles ();  for(File file2:files) {//Print OutputSYSTEM.OUT.PRINTLN (Flag (count) +file2.getname ()); //Whether the File2 is a folder or a file                if(File2.isdirectory ()) {//File2 is a folder//count++;//count = count+1//Count 2 3//continue to find recursive callsGetFile (file2,count+1);//2 count = 1                                    }                                //determine if the file needs to be deleted                if(File2.isfile ()) {deleteafterweekfile (file2); //Get total File sizeGetFileSize (file2); }                             //to determine if there is hidden data                if(Gethiddenfile (file2)) {System.out.println (File2.getname ()+ "This is a hidden file"); }            }                    }    }}

Comprehensive application of File

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.