Directory Operations Tool Class Copydir.java

Source: Internet
Author: User

  1. Package com.util;
  2. Import java.io.*;
  3. /**
  4. * 1, set up the destination directory. 2. Traverse the source directory. 3. During the traversal process, create a file or folder. Principle: Actually change the source file or directory header.
  5. * @datetime DSC 24
  6. */
  7. Public class Copydir {
  8. private File SDir, Ddir, Newdir;
  9. Public copydir (string s, String d) {
  10. This (new file (s), new file (d));
  11. }
  12. Copydir (file SDir, file Ddir)//C:\\test D:\\ABC
  13. {
  14. this.sdir = SDir;
  15. this.ddir = Ddir;
  16. }
  17. public void Copydir () throws IOException {
  18. //is to create the destination directory. That is, create a source folder to copy. Test
  19. //Gets the source folder name.
  20. String name = Sdir.getname ();
  21. //Create the folder in the destination directory by this name in order to store files or folders in the source folder.
  22. //The destination directory and the source folder name are encapsulated into a file object.
  23. Newdir = Ddir;
  24. //New File (Ddir,name);
  25. //Call the object's MkDir method. Create the folder in the destination directory. D:\\abc\\test
  26. Newdir.mkdir (); //  
  27. //Traverse the source folder.
  28. Listall (SDir);
  29. }
  30. /* 
  31. * Encapsulates the traversal directory into a method. During the traversal, the file creation file is encountered. The directory creation directory was encountered.
  32. */
  33. private void Listall (File dir) throws IOException {
  34. file[] files = dir.listfiles ();
  35. For (int x = 0; x < files.length; × x + +) {
  36. if (files[x].isdirectory ()) {
  37. Createdir (Files[x]);  //Call the method that created the directory.
  38. Listall (Files[x]); //continues to be recursive.  Enter the child directory.
  39. } Else {
  40. CreateFile (Files[x]);  //Call the method that created the file.
  41. }
  42. }
  43. }
  44. /* 
  45. * Copy directory. Create a new directory in the destination directory from the source directory.
  46. */
  47. private void Createdir (File dir) {
  48. File d = replacefile (dir);
  49. D.mkdir ();
  50. }
  51. /* 
  52. * Copy file.
  53. */
  54. private void createFile (file file) throws IOException {
  55. File NewFile = replacefile (file);
  56. the//copy file is a data transfer process.  Need to be done by flow.
  57. FileInputStream FIS = new FileInputStream (file);
  58. FileOutputStream fos = new FileOutputStream (NewFile);
  59. byte[] buf = new byte[1024x768 * 2];
  60. int num = 0;
  61. While (num = Fis.read (BUF))! =-1) {
  62. Fos.write (buf, 0, num);
  63. }
  64. Fos.close ();
  65. Fis.close ();
  66. }
  67. /* 
  68. * Replace path.
  69. */
  70. Private file Replacefile (file f) {
  71. //The principle is: Replace the parent directory (C:\\tset) of the source directory with the destination parent directory. (d:\\abc\\test)
  72. String path = F.getabsolutepath ();  //Gets the decision path of the source file or folder.
  73. //Replace the absolute path of the source file or folder with the destination path.
  74. String NewPath = Path.replace (Sdir.getabsolutepath (), Newdir
  75. . GetAbsolutePath ());
  76. //Encapsulates a new destination path into a file object
  77. File NewFile = new File (NewPath);
  78. return newFile;
  79. }
  80. }

Directory Operations Tool Class Copydir.java

Related Article

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.