Java Mobile folder and all its subfolders and subfolders, java Mobile folder files
The following simple method can be used to describe the Java Mobile folder and all its sub-files and sub-folders:
Public static void moveFolder (String oldPath, String newPath) {// copy the file copyFolder (oldPath, newPath) first; // Delete the source file, deleteDir (new File (oldPath ));}
The file should not be cut directly to prevent errors during the cut, resulting in such problems.
Copy the folder and all its sub-files and sub-folders in Java, in [Java], operations on copying all files in a folder to another folder using the file input/output stream (click to open the link) have been described in detail.
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 individual files. Therefore, you must traverse the entire folder and start from the files in the innermost folder, perform recursive deletion 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 the files in the folder String [] children = dir. list (); // recursively Delete the for (int I = 0; I <children. length; I ++) {// File f = new File (String parent, String child) // The abstract path name of parent is used to represent a directory, and the child path name String is used to represent a directory or a File. // The File path is boolean isDelete = deleteDir (new File (dir, children [I]). // if the File is deleted, nothing can be deleted, when isDelete = false, the recursive if (! IsDelete) {return false ;}}// if a file or an empty directory is read, you can directly delete return dir. delete ();}
Therefore, the entire method is like this. Move the folder under drive C and all its sub-files and sub-folders to drive 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 the files in the folder String [] children = dir. list (); // recursively Delete the for (int I = 0; I <children. length; I ++) {// File f = new File (String parent, String child) // The abstract path name of parent is used to represent a directory, and the child path name String is used to represent a directory or a File. // The File path is boolean isDelete = deleteDir (new File (dir, children [I]). // if the File is deleted, nothing can be deleted, when isDelete = false, the recursive if (! IsDelete) {return false ;}}// if a file or an empty directory is read, you can directly Delete return dir. delete ();} // copy all subdirectories and files in a directory and directory to the new folder public static void copyFolder (String oldPath, String newPath) {try {// create a new folder (new File (newPath) If the folder does not exist )). mkdirs (); // read the content of the entire folder to the file string array. Set a cursor I below, and read the array File filelist = new File (oldPath) without stopping the move ); string [] file = filelist. list (); // note that this temp is only a temporary File pointer // the entire program does not create a temporary File temp = Null; for (int I = 0; I <file. length; I ++) {// If oldPath ends with a path separator/or, then the oldPath/file name will be ready. // otherwise, you need to add a path separator after your oldPath and the file name. // who knows whether the parameter you passed is f:/a or f: /a/ah? 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 ("An error occurred while copying the entire folder"); }} public static void moveFolder (String oldPath, String newPath) {// copy the file copyFolder (oldPath, newPath) first ); // Delete the source File to avoid confusion during replication. 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 for your learning and support for helping customers.