Requirement: Copy all. Java files in one directory to another directory, including subdirectory files

Copy all. Java files to the point directory and subdirectories. Find all. Java files. To copy files, you need to use input streams and output streams

Question: How do I create subdirectories in a new directory to write files to after I find the.java file? How do I recursively create folders

Paste () creates a paste() method with no return value, and passes in a file object.

In paste(), we find the array files subdirectory of the file directory, then iterate through the array and determine if the subdirectory is a directory recursively (to find a.java file in the lower directory), if it isn’t a directory, if it ends in.java, then copy it to the target directory. And here I’m going to create another method to do that,pasteToDir();

In this method, the byte inputStream object inputStream is created, and then the directory is created.

The first step is to get the full path of the copied files. Then change the path in replace to the new directory (which is used to store the copied files).

The second step is to use the new path to construct the File class object so that we can create the directory manually using mkdirs(). After the directory is created, you can use the constructor in the File class to create a new File with the same name as the copied File. The directory and files have been created successfully; Finally, we construct the OutputStream byte OutputStream object to store the data in the corresponding file.

The main advantage of this method is that the subdirectories and subfiles can be copied to the new directory except that the initial drive name and directory name are different. In this way, the entire structure of the original file is not broken, and the file is successfully moved to the new location.

Thank you so good to study hard, read my article, if you feel useful, please light up my little star, don’t mean your praise, I will try to do better, do more valuable learning experience!!