ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileReader;Importjava.io.IOException;ImportJava.nio.file.Files;Importjava.nio.file.Paths;Importjava.util.ArrayList;Importjava.util.List; Public classClearfile {StaticString folder = "E:\\newserver\\wechat\\joint_care\\source"; List<FilePojo> filelist =NewArraylist<filepojo>(); List<FilePojo> ContentList =NewArraylist<filepojo>(); Public Static voidMain (string[] args)throwsIOException {clearfile Clear=NewClearfile (); Clear.clear (folder); } Public voidClear (String folder)throwsioexception{getallfilepaths (NewFile (folder)); Getcontentfiles (NewFile (folder)); for(Filepojo file:contentlist) {search (File.getpath ()); } System.out.println ("****************************************** deleted the following files ************************************"); for(Filepojo file:filelist) {if(!File.isfind ()) {System.out.println (File.getpath ()); NewFile (File.getpath ()). Delete (); } } } Private voidgetallfilepaths (File filePath) {file[] files=Filepath.listfiles (); for(File file:files) {if(File.isdirectory ()) {getallfilepaths (file); }Else{String filename=File.getname (); if(Filename.contains (". jpg") || Filename.contains (". png") || Filename.contains (". gif") || Filename.contains (". js")) || Filename.contains (". css") {Filepojo Pojo=NewFilepojo (); Pojo.setname (File.getname ()); Pojo.setpath (File.getpath ()); Filelist.add (Pojo); } } } } Private voidgetcontentfiles (File filePath) {file[] files=Filepath.listfiles (); for(File file:files) {if(File.isdirectory ()) {getcontentfiles (file); }Else{String filename=File.getname (); if(Filename.contains (". html") || Filename.contains (". FTL")) || Filename.contains (". css") || Filename.contains (". js")) || Filename.contains (". Java") {Filepojo Pojo=NewFilepojo (); Pojo.setname (File.getname ()); Pojo.setpath (File.getpath ()); Contentlist.add (Pojo); } } } } Public voidSearch (String filename)throwsIOException {bufferedreader br=NewBufferedReader (Newfilereader (filename)); for(String line; (line = Br.readline ())! =NULL; ) { for(Filepojo file:filelist) {if(Line.contains (File.getname ())) {File.setfind (true); }}} br.close (); } Private classfilepojo{PrivateString name; PrivateString Path; Private Booleanfind; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetPath () {returnpath; } Public voidSetPath (String path) { This. Path =path; } Public BooleanIsfind () {returnfind; } Public voidSetfind (Booleanfind) { This. Find =find; } }}
There is a small problem: If the file name is 01.jpg, Xx01.jpg is referenced in the code, then 01.jpg is considered a required file.
Clear unused images, JS, CSS code in Eclipse projects