Java Delete folders and files

Source: Internet
Author: User

Before Javaeye saw about the Java operation file of an article, write very good, but find a half-day also did not find, will find the bottom several articles to tidy up, do a summary, is a learning backup ...

1, verify that the incoming path is the correct path name (Windows system, other systems are not used)

Java code
    1. Regular expression that verifies whether a string is the correct pathname
    2. Private static String matches = "[a-za-z]:\\\\[^:?\" ><*]* ";
    3. Correct by the return value of the spath.matches (matches) method
    4. Spath as a path string

2, the common folder or file deletion method, call this method directly, you can delete the folder or file, including all the files under the folder

Java code
  1. /**
  2. * Deletes the specified directory or file according to the path, regardless of whether it exists or not
  3. * @param spath the directory or file to be deleted
  4. * @return Delete succeeds returns True, otherwise false is returned.
  5. */
  6. Public Boolean DeleteFolder (String spath) {
  7. Flag = false;
  8. FILE = new file (spath);
  9. //Determine if a directory or file exists
  10. if (!file.exists ()) { //does not exist return false
  11. return flag;
  12. } Else {
  13. //Determine if the file is
  14. if (File.isfile ()) { //is a file when the delete file method is called
  15. return DeleteFile (spath);
  16. The Delete directory method is called when the else {//is a directory
  17. return DeleteDirectory (spath);
  18. }
  19. }
  20. }

3, to implement the method of deleting files,

Java code
  1. /**
  2. * Delete individual files
  3. * @param the file name of spath deleted files
  4. * @return Single File Delete successfully returns TRUE, otherwise false
  5. */
  6. Public Boolean DeleteFile (String spath) {
  7. Flag = false;
  8. FILE = new file (spath);
  9. //path is file and is not empty then delete
  10. if (file.isfile () && file.exists ()) {
  11. File.delete ();
  12. Flag = true;
  13. }
  14. return flag;
  15. }

4, implement the method of deleting the folder,

Java code
  1. /**
  2. * Delete directories (folders) and files in directory
  3. * @param spath The file path of the deleted directory
  4. * @return Directory Delete successfully returns TRUE, otherwise false
  5. */
  6. Public boolean deletedirectory (String spath) {
  7. //If spath does not end with a file delimiter, the file delimiter is automatically added
  8. if (!spath.endswith (File.separator)) {
  9. spath = spath + file.separator;
  10. }
  11. File Dirfile = new File (spath);
  12. //If the dir corresponding file does not exist or is not a directory, exit
  13. if (!dirfile.exists () | |!dirfile.isdirectory ()) {
  14. return false;
  15. }
  16. Flag = true;
  17. //Delete all files under the folder (including subdirectories)
  18. file[] files = dirfile.listfiles ();
  19. For (int i = 0; i < files.length; i++) {
  20. //Delete sub-Files
  21. if (Files[i].isfile ()) {
  22. Flag = DeleteFile (Files[i].getabsolutepath ());
  23. if (!flag) break ;
  24. } //delete sub-directories
  25. else {
  26. Flag = DeleteDirectory (Files[i].getabsolutepath ());
  27. if (!flag) break ;
  28. }
  29. }
  30. if (!flag) return false;
  31. //Delete current directory
  32. if (Dirfile.delete ()) {
  33. return true;
  34. } Else {
  35. return false;
  36. }
  37. }

5,main () method

Java code
  1. Public static void Main (string[] args) {
  2. Handlefileclass HFC = new Handlefileclass ();
  3. String path = "D:\\abc\\123\\ab1";
  4. Boolean result = HFC.  CreateFolder (path);
  5. SYSTEM.OUT.PRINTLN (result);
  6. Path = "d:\\abc\\124";
  7. result = HFC. DeleteFolder (path);
  8. SYSTEM.OUT.PRINTLN (result);
  9. }

The main () method only makes a simple test, the establishment of folders and files are locally established, the situation should be considered very comprehensive, including folders containing folders, files. Different cases of the file .....

There are no problems with the implementation, and folders and files can be deleted correctly.

For other types of files, continue to learn .....

Java Delete folders and files

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.