Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to move folders in Java

2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to move folders in Java". In daily operation, I believe many people have doubts about how to move folders in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to move folders in Java". Next, please follow the editor to study!

The mobile folder in Java and all its subfiles and subfolders can be explained in a simple way as follows:

Public static void moveFolder (String oldPath, String newPath) {/ / copy the file copyFolder (oldPath, newPath) first; / / delete the source file so as not to confuse deleteDir (new File (oldPath));}

Files should not be cut directly to prevent errors in cutting, causing problems of one kind or another.

To copy a folder and all its subfiles and subfolders in Java, the key is to delete the folder and its subfiles and subfolders.

In Java, the delete () method of the File class can only delete empty folders or individual files, so you must traverse the entire folder, starting with the files in the innermost folder and deleting them recursively as follows:

/ / delete a directory and all subdirectories and files under the directory public static boolean deleteDir (File dir) {/ / if it is a folder if (dir.isDirectory ()) {/ / then read out all the files under the folder String [] children = dir.list (); / / Recursively delete for (int I = 0; I < children.length) under the subdirectory in the directory ) {/ / File f=new File (String parent, String child) / / parent abstract pathname is used to represent a directory, and the child pathname string is used to represent a directory or file. / / it happens to be the file path boolean isDelete= deleteDir (new File (dir, child [I])); / / if there is nothing to delete and isDelete==false, then the recursive if (! isDelete) {return false;} / / you can delete return dir.delete () directly if you read a file or an empty directory;}

So, this is what the whole method looks like, move the A folder under disk C and all its subfiles and subfolders to disk F, and rename it:

Import java.io.*; public class CutTest {/ / Delete a directory and all subdirectories and files under the directory public static boolean deleteDir (File dir) {/ / if it is a folder if (dir.isDirectory ()) {/ / then read out all files under that folder String [] children = dir.list () / / Recursively delete the for (int I = 0; I < children.length; iTunes +) {/ / File f=new File (String parent, String child) / / parent abstract pathname under the subdirectory in the directory, and the child pathname string is used to represent the directory or file. / / it happens to be the file path boolean isDelete= deleteDir (new File (dir, child [I])); / / if the deletion is finished and there is nothing to delete, isDelete==false, then jump out of the recursive if (! isDelete) {return false } / / if you read a file or an empty directory, you can delete return dir.delete () directly. } / / copy a directory and all subdirectories and files under the directory to a 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 keep moving down to start reading the array File filelist = new File (oldPath); String [] file = filelist.list (); / / Note that this temp is only a temporary file pointer / / the whole program does not create a temporary file File temp = null For (int I = 0; I < file.length; I +) {/ / if oldPath ends with a path delimiter / or\, then the oldPath/ file name is fine / / otherwise you have to add a path delimiter followed by your own oldPath followed by the file name / / who knows whether the parameter you passed is ffile.length; oldPath an 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 the 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 the folder if (temp.isDirectory ()) {copyFolder (oldPath + "/" + file [I], newPath + "/" + file [I])) } catch (Exception e) {System.out.println ("error copying the contents of the entire folder");}} public static void moveFolder (String oldPath, String newPath) {/ / copy the file copyFolder (oldPath, newPath) first; / / delete the source file so as not to confuse deleteDir (new File (oldPath)) when copying } public static void main (String [] args) {moveFolder ("Java args A", "FRV peg B");}} at this point, the study on "how to move a folder in Java" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report