- Package com.util;
- Import java.io.*;
- /**
- * 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.
- * @datetime DSC 24
- */
- Public class Copydir {
- private File SDir, Ddir, Newdir;
- Public copydir (string s, String d) {
- This (new file (s), new file (d));
- }
- Copydir (file SDir, file Ddir)//C:\\test D:\\ABC
- {
- this.sdir = SDir;
- this.ddir = Ddir;
- }
- public void Copydir () throws IOException {
- //is to create the destination directory. That is, create a source folder to copy. Test
- //Gets the source folder name.
- String name = Sdir.getname ();
- //Create the folder in the destination directory by this name in order to store files or folders in the source folder.
- //The destination directory and the source folder name are encapsulated into a file object.
- Newdir = Ddir;
- //New File (Ddir,name);
- //Call the object's MkDir method. Create the folder in the destination directory. D:\\abc\\test
- Newdir.mkdir (); //
- //Traverse the source folder.
- Listall (SDir);
- }
- /*
- * Encapsulates the traversal directory into a method. During the traversal, the file creation file is encountered. The directory creation directory was encountered.
- */
- private void Listall (File dir) throws IOException {
- file[] files = dir.listfiles ();
- For (int x = 0; x < files.length; × x + +) {
- if (files[x].isdirectory ()) {
- Createdir (Files[x]); //Call the method that created the directory.
- Listall (Files[x]); //continues to be recursive. Enter the child directory.
- } Else {
- CreateFile (Files[x]); //Call the method that created the file.
- }
- }
- }
- /*
- * Copy directory. Create a new directory in the destination directory from the source directory.
- */
- private void Createdir (File dir) {
- File d = replacefile (dir);
- D.mkdir ();
- }
- /*
- * Copy file.
- */
- private void createFile (file file) throws IOException {
- File NewFile = replacefile (file);
- the//copy file is a data transfer process. Need to be done by flow.
- FileInputStream FIS = new FileInputStream (file);
- FileOutputStream fos = new FileOutputStream (NewFile);
- byte[] buf = new byte[1024x768 * 2];
- int num = 0;
- While (num = Fis.read (BUF))! =-1) {
- Fos.write (buf, 0, num);
- }
- Fos.close ();
- Fis.close ();
- }
- /*
- * Replace path.
- */
- Private file Replacefile (file f) {
- //The principle is: Replace the parent directory (C:\\tset) of the source directory with the destination parent directory. (d:\\abc\\test)
- String path = F.getabsolutepath (); //Gets the decision path of the source file or folder.
- //Replace the absolute path of the source file or folder with the destination path.
- String NewPath = Path.replace (Sdir.getabsolutepath (), Newdir
- . GetAbsolutePath ());
- //Encapsulates a new destination path into a file object
- File NewFile = new File (NewPath);
- return newFile;
- }
- }
Directory Operations Tool Class Copydir.java