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