Java realizes the files of reading and deleting the folder
package test.com; Import java.io.file; Import Java.filenotFoundexception; Import Java.io.ioException; Public Class ReadFile {Public le () {} /*** Read all files under a folder */ Public Static Boolean Readfile (String FilePath) Throws FilenotFoundexception, IOEXCEPTION {File File = New File (FilePath); if (! File.isDirectory ( )) {// system.out.println ("file");/ / System.out.println ("path =" + file.getpath ()); // System.out.println ("absolutepath =" + file.getabsolutepath ()); tname () );} else if (file.isdirectory ()) {string [] filelist = file.list (); for (int i = 0; i <<<<<<<<filelist.leng; I ++)) {file read file (filepath+"/// /" + filelist [i]); if (! Readfile.isdirectory ()) {// System.out.println (" PATH = " + Readfile.getpath ()); // System.out.println (" absolutePath = "// + readfile.getabsolutepath ()); System.out.println (ReadFile.getName ());} Else if (ReadFile.isdirectory ()) {ReadFile (FilePath +"/" + FIs Lelist [i]; }}} Catch (FilenotFoundException E) {System.out.println ("ReadFile () Exception:" + E.getMessage ());} Return True;} /*** Delete a folder All folders below And file* / /*public static boyan deletefile (String Delpath) Throws FilenotFoundException, IOEXCEPTION {File File = New File (Delpath); if (! File.isdire ctory ()) {system.out.println ("1") ; file.delete ();} else if (file.isdirectory ()) {system.out.println ("2"); string [] filelist = file.list (); for (int i = 0; .Length; I ++) {file deelfile = new file (delpath + "//" + filelist [i]); if (! Delfile.isdirectory ()) {system.out.println ("path =" + deldPath (" ); System.out.println ("absolutepath =" + Delfile.getabsolutepath ()); System.out.println ("name =" + deldile.getName ()); Delfile.Delete (); YSTM.OUT.PRINTLN ("删除文件成功"); } else if (delfile.isDirectory()) { deletefile(delpath + "//" + filelist[i]); } } file.delete(); } } catch (FileNotFoundException e) { System.out.println ("Deletefile () Exception:" + E.getMessage ());} Return True;}*/ Public Static void Main (string [] arts) {Try {c : // users/ /// desktop // skj_h25 positive // 004_rcag // 003_skj "); // Deletefile (" d:/file ");} Catch (FILENOTFOUNDEPTION EX) {} Catch (IOEXCEPTION EX) {} STEM.out.println (OK ");}}
Method two:
Package Othertudy; Import Java.iO.File; Import Java.filenotFoundexception; Import Java.io.ioException; @Createtime: AUG 1, 2014 11:42:44 am * @Author: Mayi * @Descripting: Read and delete all files under the folder * */Public Class TestReadFile {/** * The absolute path of the webroot of the project * @Return */String "// d:/${workspace}/${projectName}/WebRoot/WEB-INF/classes/"的路径String path=this.getClass().getResource("/").getPath(); //从路径字符串Take out the engineering path path = path.substring (1, path.indexof ("web-inf/class"); System.out.println ("Engineering Path:"+PATH); Return Path;}/*** @ @ @ @ @ @ Param ARGS */Public Static void main (string [] args) {testreamFile trf = new testreamFile (); string xmlpath = trf.getProjectPath ()+ "testocs"; {Readallfile (XMLPATH);} Catch (FilenotFoundexception E) {{ e.printstacktrace ();} Catch (IOEXCEPTION E) {e.printstacktrace ();}/** * All files under the specified path folder * @param filepath * @Return * @Throws filenotfffffff Oundexception * @throws IOEXception */Public Static Boolean Readallfile (String FilePath) Throws FilenotFoundException, IOEXCEPTION {File File = New File (FilePath); if (! File.isDirect exit ()) {system.out.println ("/n file information:") ; System.out.println ("/T relative path =" + file.getpath ()); system.out.println ("/t absolute path =" + file.getabsolutepath ()); syster.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 readful = filelist [i]; if (! Readfile.isdirectory ()) {System.out.println ("/N/N/N/N/N/N/N/N/N/N/N/N/N/N/N/N/N/N/N/N/N/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 ());}}} {System.out.pr Intln ("ReadFile () Exception : " + E.getMessage ());} Return True;}/** * Delete all folders and files under a folder * @param Delpath * @Return * @THROWS FILENOTFOUNDEPTION * @Throws IOEXception */Public Static Boolean Deletefile (String Delpath) Throws FilenotFoundexception, IOEXCEPTION {Try {File File = New File (DELPATH); IF (! File.isdirectory ()) {System.out.p rintln ("1"); File.delete ();} else if (file.isdirectory ()) {System.out.println ("2"); Filerist = File.ListFiles (); for (int i = 0; I <<<TE ++) {File Delfile = filelist [i]; if (! Delfile.isdirectory ()) {system.out.println ("relative path =" + deldile.getpath ()); epath ()); System.out.println ("Full name of the file =" + Delfile.getName ()); Delfile.Delete (); System.out.println ("Delete File success");} else if (Delfile.isDirectory ()) {deletefile (filelist [i] .getpath ());}} File.delete ();}}} (FILENOTFOUNDEXCEPTION E) {System.out.println ("Deletefile ()" e.getMessage ( );} Return true;}}
The above is all the contents of this article. I hope everyone can like it.