Package otherstudy; Import java. io. File; Import java. io. FileNotFoundException; Import java. io. IOException; /** * @ ClassName: TestReadFile * @ CreateTime: Aug 1, 2014 11:42:44 AM * @ Author: mayi * @ Description: Reads and deletes all files in the folder. * */ Public class TestReadFile { /** * Obtain the absolute WebRoot path of the project. * @ Return */ String getProjectPath (){ // Obtain the path such as "/d:/$ {workspace}/$ {projectName}/WebRoot/WEB-INF/classes /" String path = this. getClass (). getResource ("/"). getPath (); // Extract the project path from the path string Path = path. substring (1, path. indexOf ("WEB-INF/classes ")); System. out. println ("project path:" + path ); Return path; } /** * @ Param args */ Public static void main (String [] args ){ TestReadFile trf = new TestReadFile (); String xmlPath = trf. getProjectPath () + "testDocs "; Try { ReadAllFile (xmlPath ); } Catch (FileNotFoundException e ){ E. printStackTrace (); } Catch (IOException e ){ E. printStackTrace (); } } /** * Read all files in the specified path folder * @ Param filepath * @ Return * @ Throws FileNotFoundException * @ Throws IOException */ Public static boolean readAllFile (String filepath) Throws FileNotFoundException, IOException { Try { File file = new File (filepath ); If (! File. isDirectory ()){ System. out. println ("\ n file information :"); System. out. println ("\ t Relative Path =" + file. getPath ()); System. out. println ("\ t absolute path =" + file. getAbsolutePath ()); System. out. println ("\ t file full name =" + file. getName ()); } Else if (file. isDirectory ()){ System. out. println ("\ n folder file list information :"); File [] fileList = file. listFiles (); For (int I = 0; I <fileList. length; I ++ ){ File readfile = fileList [I]; If (! Readfile. isDirectory ()){ System. out. println ("\ n \ t Relative Path =" + readfile. getPath ()); System. out. println ("\ t absolute path =" + readfile. getAbsolutePath ()); System. out. println ("\ t file full name =" + readfile. getName ()); } Else if (readfile. isDirectory ()){ ReadAllFile (fileList [I]. getPath ()); } } } } Catch (FileNotFoundException e ){ System. out. println ("readfile () Exception:" + e. getMessage ()); } Return true; } /** * Delete all folders and files in a folder * @ Param delpath * @ Return * @ Throws FileNotFoundException * @ Throws IOException */ Public static boolean deleteFile (String delpath) Throws FileNotFoundException, IOException { Try { File file = new File (delpath ); If (! File. isDirectory ()){ System. out. println ("1 "); File. delete (); } Else if (file. isDirectory ()){ System. out. println ("2 "); File [] fileList = file. listFiles (); For (int I = 0; I <fileList. length; I ++ ){ File delfile = fileList [I]; If (! Delfile. isDirectory ()){ System. out. println ("Relative Path =" + delfile. getPath ()); System. out. println ("absolute path =" + delfile. getAbsolutePath ()); System. out. println ("file full name =" + delfile. getName ()); Delfile. delete (); System. out. println ("File deleted successfully "); } Else if (delfile. isDirectory ()){ DeleteFile (fileList [I]. getPath ()); } } File. delete (); } } Catch (FileNotFoundException e ){ System. out. println ("deletefile () Exception:" + e. getMessage ()); } Return true; } } |