1. Basic Objectives
Use Java to complete the following operations:
Copy all files in one folder to another folder, for example, there are two folders a and b in disk F:
There are a bunch of files in f:/a. After running the Java program, all of them will be copied to f:/b and renamed. Add the prefix of rename_ before all files. If there is a folder inside, the folder will not be renamed. The files inside will be renamed, and the prefix of rename_ before all files:
2. Production process
1. First of all, the main function is very simple, which is to call the copyFolder function in the FileTest class above.
public class FileCopy { public static void main(String args[]) { new FileTest().copyFolder("f:/a", "f:/b"); } }It is worth noting that in Java, f:/a has no problem, and f:/a is also no problem, but since / must be transferred when expressing a string, you must write it as f://a
2. The key to the entire program is the copyFolder function in the FileTest class. This function is -_- in this class! Also, please note that java.io.* is introduced at the beginning of the program; since the input and output stream is used
class FileTest { public 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 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, you have to add a path separator after oldPath and add a file name//Who knows whether the parameters you passed are 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); 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"); } } }It may be a bit difficult to understand when the cursor encounters a file. In fact, it is like this. First, set a file input stream, specify input from the file encountered by the cursor, and then specify output to the file directory of newPath/rename_old file name. After that, set a buffer array. For the file input stream, for the file you want to read, each time you call the read method, it will continue to read the content of the buffer array length of the buffer array bufferarray to the position you read, store the read content to the buffer array, overwrite all the contents before the buffer array, and then the file output stream will output all the contents of the buffer array at the specified location until the file input stream encounters -1.
As for why the file input stream can be in order in this way, it will continue to read backwards to the location where the last read is, because when the file is to be read, the Java encapsulated FileInputStream.read method will also call the operating system API to read these data in turn. When reading file data, it must be in sequential. It is impossible to read the first byte first and then the second to last byte. When reading loop, the read method will read the position ++, so that each read reads the following bytes sequentially until the end of the file is encountered.
When the cursor encounters a folder, just call it again to complete the same operation. This is called iteration.
3. Therefore, the whole procedure is as follows:
import java.io.*; /** * * @param oldPath The directory to be copied* @param newPath The directory to be copied* */ class FileTest { public 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 down constantly to start reading this 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, you have to add a path separator after oldPath and add a file name//Who knows whether the parameters you passed are 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); 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 class FileCopy { public static void main(String args[]) { new FileTest().copyFolder("f:/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.