File/Directory part processing tool class Dealdir.java

Source: Internet
Author: User

  1. Package com.util;
  2. Import Java.io.File;
  3. Import Java.util.StringTokenizer;
  4. /**
  5. * File/Directory part processing
  6. * @createTime Dec, 7:06:58 AM
  7. * @version 1.0
  8. */
  9. Public class Dealdir {
  10. /** 
  11. * Get the suffix of the file and convert it to uppercase
  12. *
  13. * @param fileName
  14. * File name
  15. * @return
  16. */
  17. Public string Getfilesuffix (String fileName) throws Exception {
  18. return filename.substring (Filename.lastindexof (".") + 1,
  19. Filename.length ()). toUpperCase ();
  20. }
  21. /** 
  22. * Create multi-level catalogs
  23. *
  24. * @param path
  25. * Absolute path to Directory
  26. */
  27. public void Createmultileveldir (String path) {
  28. try {
  29. StringTokenizer st = New StringTokenizer (Path, "/");
  30. String path1 = st.nexttoken () + "/";
  31. String path2 = path1;
  32. While (St.hasmoretokens ()) {
  33. path1 = St.nexttoken () + "/";
  34. path2 + = path1;
  35. File Inbox = new file (path2);
  36. if (!inbox.exists ())
  37. Inbox.mkdir ();
  38. }
  39. } catch (Exception e) {
  40. System.out.println ("Directory creation failed" + E);
  41. E.printstacktrace ();
  42. }
  43. }
  44. /** 
  45. * Delete files/directories (recursively delete files/directories)
  46. *
  47. * @param path
  48. * Absolute path to file or folder
  49. */
  50. public void DeleteAll (String dirpath) {
  51. if (Dirpath = = null) {
  52. System.out.println ("directory is empty");
  53. } Else {
  54. File path = new file (Dirpath);
  55. try {
  56. if (!path.exists ())
  57. return; //directory does not exist exit
  58. if (Path.isfile ()) //If the file is deleted
  59. {
  60. Path.delete ();
  61. return;
  62. }
  63. file[] files = path.listfiles (); //If files are deleted recursively in the directory
  64. For (int i = 0; i < files.length; i++) {
  65. DeleteAll (Files[i].getabsolutepath ());
  66. }
  67. Path.delete ();
  68. } catch (Exception e) {
  69. System.out.println ("File/directory Deletion failed" + E);
  70. E.printstacktrace ();
  71. }
  72. }
  73. }
  74. /** 
  75. * File/directory Rename
  76. *
  77. * @param OldPath
  78. * Original path (absolute path)
  79. * @param NewPath
  80. * Update Path
  81. * @author LYF NOTE: You cannot modify the upper level of the table of contents
  82. */
  83. public void Renamedir (String oldpath, String newpath) {
  84. File OldFile = new File (OldPath); File or directory
  85. File NewFile = new File (NewPath); File or directory
  86. try {
  87. Boolean success = Oldfile.renameto (NewFile); //Rename
  88. if (!success) {
  89. System.out.println ("rename failed");
  90. } Else {
  91. System.out.println ("renaming succeeded");
  92. }
  93. } catch (RuntimeException e) {
  94. E.printstacktrace ();
  95. }
  96. }
  97. }

File/Directory part processing tool class Dealdir.java

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.