Java uses the file input/output stream to copy all files in the folder to another folder.
I. Basic Objectives
Use Java to complete the following operations:
Copy all files in a folder to another folder. For example, there are two folders, a and B, in drive F:
F:/a contains a pile of files. After the Java program is run, all files will be copied to f:/B and renamed. The rename _ prefix will be added before all files, if a folder exists, the folder is not renamed, and the files in the folder are renamed. The rename _ prefix is also added before all files:
Ii. Production Process
1. First, the main function is very simple, that is, the copyFolder function in the FileTest class above is called.
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 is correct, and f: \ a is normal, however, because \ must be transferred during string expression, you must write it as f: \
2. The key of the entire program is the copyFolder function in the FileTest class. This function is used in this class -_-! Note that java. io. * is introduced at the beginning of the program. The input and output streams are used.
Class FileTest {public 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 the oldPath ends with a path separator/or \, The oldPath/file name will be ready. // otherwise, you need to follow the oldPath Add a path separator and a file name. // who knows whether the parameter you passed is f:/a or f:/? 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 (pre Readlength = 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 ");}}}
The cursor may be difficult to understand when it encounters a file. In fact, this is the case. First, set the input stream of a file to specify the input from the file encountered by the cursor, specify the file directory that is output to the newPath/rename _ old file name, and then set a buffer array. The file input stream calls the read method each time for the file to be read, it will continue to read the buffer array bufferarray length content from the last read position, and store the read content to the buffer array to overwrite all the content before the buffer array, then, the file output stream outputs all the contents of the buffer array at the specified position until the file input stream encounters-1.
As to why the file input stream can be read in this order, it will continue to read the last read position each time, because when the file is to be read, The FileInputStream encapsulated by Java. the read method also calls the API of the operating system to read the data in sequence. When reading file data, it must be sequential. It is impossible to read the first byte first and then the second to the last byte. During Cyclic reading, the read method reads the position ++. Therefore, each read operation reads the subsequent bytes sequentially until the end of the file is marked.
When a cursor encounters a folder, you can call it again to complete the same operation. This is called iteration.
3. The entire program is as follows:
Import java. io. *;/***** @ param oldPath directory to be copied * @ param newPath directory to be copied */class FileTest {public 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); FileOutputStream output = new FileOutputStream (newPath + "/" + "rename _" + (temp. getName ()). toString (); byte [] bufferarray = new byte [1024*64]; int prereadlength; while (pre Readlength = 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 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 for your learning and support for helping customers.