"Java" moves the folder and all its child files and subfolders

Source: Internet
Author: User

In the Java Mobile folder and all of its child files and subfolders can have the following simple way to illustrate:

public static void MoveFolder (String oldpath, String NewPath) {//Copy file CopyFolder (OldPath, NewPath) First,//delete source file, So as not to copy the time of the confusion Deletedir (new File (OldPath));}

You should not cut files directly to prevent errors when cutting, causing problems like this.

In the Java Copy folder and all its sub-files and subfolders, the "Java" uses the file input and output stream to complete the operation of a folder to copy all the files within a folder (click the Open link) in the article has been described in detail.

The key is to delete the folder and its sub-files and subfolders.

In Java, the Delete () method of the file class can only be deleted as an empty folder or a single file, so you must traverse the entire folder, starting with the files in the top-level folder and recursively deleting them, as follows:

Delete all subdirectories and files under a directory and directory public static Boolean deletedir (file dir) {///If it is a folder if (Dir.isdirectory ()) {// Read all files under the folder string[] children = dir.list ();//recursively delete subdirectories in directory for (int i = 0; i < children.length; i++) {//File f=new Fi Le (string parent, String child)//The parent abstract pathname is used to represent the directory, and the child pathname string is used to represent the directory or file. It just happens to be the file path, Boolean isdelete = Deletedir (new file (dir, children[i]);//If it is deleted, nothing is deleted, Isdelete==false, then jump out at this time recursion if (!). Isdelete) {return false;}}} Read a file or an empty directory, you can directly delete the return Dir.delete ();}
So, the whole method is like this, move the a folder under C drive and all its sub-files and subfolders to the F-disk, and rename:

Import java.io.*;p Ublic class Cuttest {//delete all subdirectories and files under a directory and directory public static Boolean deletedir (file dir) {///If it is a folder if (dir.i  Sdirectory ()) {//read out all files under the folder string[] children = dir.list ();//recursively delete subdirectories in directory for (int i = 0; i < children.length; i++) {//File F=new file (string parent, String child)//The parent abstract pathname is used to represent the directory, and the child pathname string is used to represent the directory or file. It just happens to be the file path, Boolean isdelete = Deletedir (new file (dir, children[i]);//If it is deleted, nothing is deleted, Isdelete==false, then jump out at this time recursion if (!). Isdelete) {return false;}}} Read a file or an empty directory, you can directly delete the return Dir.delete ();} Copy all subdirectories and files under a 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 Le (NewPath)). Mkdirs ();//Read the contents of the entire folder into the file string array, set a cursor I below, and keep moving down to start reading the array file FileList = new file (OldPath); string[] File = Filelist.list ();//To note that this temp is just a temporary file pointer//whole program and does not create temporary file temp = null;for (int i = 0; i < File.lengt H i++) {///if OldPath with the path delimiter/or \ End, then the oldpath/file name can be//otherwise you have to OldPath after the path delimiter and add the file name//Who knows you pass the parameter 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 Fi Leoutputstream (newpath+ "/" + "rename_" + (Temp.getname ()). ToString ()); byte[] Bufferarray = new byte[1024 * 64];int Prerea Dlength;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 copying entire folder contents operation");}} public static void MoveFolder (String oldpath, String NewPath) {//Copy file CopyFolder (OldPath, NewPath) First,//delete source file, So as not to copy the time of the confusion Deletedir (new File (OldPath));} public static void Main (string[] args) {MoveFolder ("c:/a", "f:/b");}}




"Java" moves the folder and all its child files and subfolders

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.