Java implementation read and delete files under a folder

Source: Internet
Author: User
Tags getmessage readfile relative

This article to share is the Java implementation read, delete files under the folder, where File.delete () used to delete "a file or an empty directory"! So to delete a directory and all the files and subdirectories in it, to be recursive delete, the need for small partners to refer to.

Java implementation read and delete files under a folder

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 Package test.com;   Import Java.io.File; Import java.io.FileNotFoundException; Import java.io.IOException;   public class ReadFile {public ReadFile () {}/** * read all files under a folder */public static Boolean ReadFile (String filepath Throws FileNotFoundException, IOException {try {  file = new file (filepath); if (!file.isdirectory ()) {//S Ystem.out.println ("document"); System.out.println ("path=" + File.getpath ()); System.out.println ("absolutepath=" + File.getabsolutepath ()); System.out.println (File.getname ());  } else if (File.isdirectory ()) {string[] filelist = File.list (); for (int i = 0; i < filelist.length; i++) {Fi Le ReadFile = new 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 + "" + Filelist[i]);}  } &nbsp catch (FileNotFoundException e) {System.out.println ("ReadFile () Exception:" + e.getmessage ());} return true; }  /** * Delete all folders and files under a folder * *    /*public static Boolean DeleteFile (String delpath) throws Filenotfoundex Ception, IOException {try {  file = new file (Delpath); if (!file.isdirectory ()) {System.out.println ("1"); fil E.delete (); else if (file.isdirectory ()) {System.out.println ("2"); string[] filelist = File.list (); for (int i = 0; i < filelist.length i++) {file Delfile = new File (Delpath + "" + filelist[i]); if (!delfile.isdirectory ()) {System.out.println ("path=" + Delfile.getpath ()); System.out.println ("absolutepath=" + Delfile.getabsolutepath ()); System.out.println ("name=" + delfile.getname ()); Delfile.delete (); System.out.println ("Delete file succeeded"); 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[] args) {try {ReadFile ("c:usersswdesktopskj_h25 patch 04_rcag03_skj");//Deletefil E ("D:/file"); The catch (FileNotFoundException ex) {} catch (IOException ex) {} System.out.println ("OK"); }  }

Method Two:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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: Read and delete all text under folder * */public class Testreadfile {/** * acquisition of the Webroot of the Project absolute path * @return/String GetProjectPath () {//Get form like "/d:/${workspace}/$" {projectname}/webroot/web-inf/classes/path String path=this.getclass (). GetResource ("/"). GetPath (); Remove the engineering path from the path string path=path.substring (1, Path.indexof ("web-inf/classes")); SYSTEM.OUT.PRINTLN ("Engineering 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.printstac Ktrace (); /** * Reads all files under the specified path folder * @param filepath * @return * @throws filenotfoundexception * @throws ioexception/Public stat IC Boolean ReadAllfile (String filepath) throws FileNotFoundException, IOException {try {File file = new file (filepath); if (!file.isdir Ectory ()) {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.prin TLN ("nt 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 under 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.printl N ("Relative path =" + Delfile.getpath ()); SYSTEM.OUT.PRINTLN ("absolute path =" + Delfile.getabsolutepath ()); System.out.println ("file Full name =" + Delfile.getname ()); Delfile.delete (); System.out.println ("Delete file succeeded"); else if (delfile.isdirectory ()) {DeleteFile (Filelist[i].getpath ());}} File.delete (); } catch (FileNotFoundException e) {System.out.println ("DeleteFile () Exception:" + e.getmessage ());} return true; } }

The above is the entire contents of this article, I hope you can enjoy.

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.