In Java, there is a simple way to illustrate the following:
public static void moveFolder(String oldPath, String newPath) { //Copy the file copyFolder(oldPath, newPath); //Delete the source file to avoid confusing deleteDir(new File(oldPath)); } Files should not be cut directly to prevent errors during cutting, resulting in problems of one kind or another.
Copying folders and all subfiles and subfolders in Java has been explained in detail in the article "[Java] uses file input and output streams to complete the operation of copying all files in one folder and another folder" (click to open the link).
The key is to delete folders, their subfiles and subfolders.
In Java, the delete() method of the File class can only delete empty folders or single files. Therefore, it is necessary to traverse the entire folder, start with the files in the innermost folder, and perform recursive deletion. The specific method is as follows:
// Delete all subdirectories and files in a directory and directory public static boolean deleteDir(File dir) { // If it is a folder if (dir.isDirectory()) { // Read all files in that folder String[] children = dir.list(); // Recursively delete the subdirectories in the directory for (int i = 0; i < children.length; i++) { // File f=new File(String parent, String child) // Parent abstract path name is used to represent directories, and child path name string is used to represent directories or files. // The file path is exactly the same as the file path. boolean isDelete = deleteDir(new File(dir, children[i])); // If you delete it and there is nothing to delete it, and isDelete==false, then the recursion will pop up if (!isDelete) { return false; } } } // What you read is a file or an empty directory, you can directly delete return dir.delete(); } Therefore, this is how the whole method is connected, move the A folder and all subfiles and subfolders under disk C to disk F, and rename it:
import java.io.*; public class CutTest { // Delete all subdirectories and files in a directory and directory public static boolean deleteDir(File dir) { // If it is a folder if (dir.isDirectory()) { // Read all files in that folder String[] children = dir.list(); // Recursively delete the subdirectories in the directory for (int i = 0; i < children.length; i++) { // File f=new File(String parent, String child) // Parent abstract path name is used to represent directories, and child path name string is used to represent directories or files. // Connected together is the file path boolean isDelete = deleteDir(new File(dir, children[i])); // If you delete it and there is nothing to delete it, and isDelete==false, then recursively jump out of this time if (!isDelete) { return false; } } } // If you read a file or an empty directory, you can directly delete return dir.delete(); } // Copy all subdirectories and files in a certain directory and directory to the new folder public static void copyFolder(String oldPath, String newPath) { try { // If the folder does not exist, create a new folder (new) File(newPath)).mkdirs(); // Read the contents of the entire folder to the file string array, set a cursor i below, and move it downwards to start reading the array File filelist = new File(oldPath); String[] file = filelist.list(); // Note that this temp is just a temporary file pointer// The entire program does not create a temporary file File temp = null; for (int i = 0; i < file.length; i++) { // If oldPath ends with a path separator/or/, then oldPath/file name is fine// Otherwise, add a path separator after oldPath and add a file name// Who knows whether the parameter you passed is f:/a or f:/a/? if (oldPath.endsWith(File.separator)) { temp = new File(oldPath + file[i]); } else { temp = new File(oldPath + File.separator + file[i]); } // If the cursor encounters a file if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); // Copy and rename FileOutputStream output = new FileOutputStream(newPath + "/" + "rename_" + (temp.getName()).toString()); byte[] bufferarray = new byte[1024 * 64]; int prereadlength; while ((prereadlength = input.read(bufferarray)) != -1) { output.write(bufferarray, 0, prereadlength); } output.flush(); output.close(); input.close(); } // If the cursor encounters a folder if (temp.isDirectory()) { copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { System.out.println("Error in copying the entire folder content"); } } public static void moveFolder(String oldPath, String newPath) { // Copy the file first copyFolder(oldPath, newPath); // Delete the source file to avoid confusing deleteDir(new File(oldPath)); } public static void main(String[] args) { moveFolder("c:/A", "f:/B"); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.