Implementation of fast batch file movement in Java

File movement is a common operation in computer resource management, which can be realized by cutting and copying files or dragging the mouse in the operating system. However, in the programming implementation of java files, most of them are realized by copying files to the destination and then deleting all files. This is not a disadvantage for small files, but moving several large files will slow down the operation and waste system resources. This example will directly realize the rapid movement of files through the renameto () method of file class. Even moving a few GB of files will not need to wait too long.

Train of thought analysis:

The first is the view layer. Here is a suggestion, because in the event of some controls, other controls are often accessed, and the parameters used in the event method of the control are almost fixed ActionEvent classes, and other parameters are rarely passed. Therefore, even if the view is automatically generated by dragging controls, these controls should be set as member variables of the class in the code. In this example, JPanel control is used as the container of other controls, jlabel control is used to display fixed information, jtextfield control is used to display files and target folders to be moved, JButton control is used to select source folders, target folders and programs to move and close, and jscrollpanel is used to display bar columns, And jtextarea control to display operation records. Then the model layer. For the Browse button, to obtain the file name array in the source folder and the path of the target folder, you need to define a file type array member variable to save the file name, and then define a file type member variable to save the target path. After selecting the Browse button of the source file, first create a Jfilechooser file selector and use setmultiselectionenabled (true) of the Jfilechooser class; You can select multiple methods through showopendialog (this) of Jfilechooser class; Method to display the file selection dialog box. If the user confirms, use the getselectedfiles() method of Jfilechooser class to obtain the selected file array and assign it to the previously defined file type array member variable. Clear the text box through the settext() method of jtextfield() class to remove the record of the last operation, create a new StringBuilder object, and use foreach() to cycle through the file array, Connect the file name through the append() method of the StringBuilder class, because there are multiple "," in the front, Then use the substring () method of StringBuilder class to obtain the string of all file names, and display the file name string to the text box through the settext () method of jtextfieldl class. For the Browse button to select the target folder, first create a Jfilechooser file selector, and use the setfileselectionmode of Jfilechooser class () method sets that the selector is only effective for folders. The file opening dialog box is displayed through the showopendialog () method of Jfilechooser class, and the getselectedfile() method of Jfilechooser class is used to obtain the selected folder, Finally, use the settext() method of jtextfield control to display the folder to the text box. For the event processing method of move button, first use the length attribute of the array to judge whether there are elements in the file array. If so, use foreach() to cycle through the file array, create a move target file for each file element in the array, and use the append of jtextarea control The () method displays the move record, uses the renameto () method of the file class to move the file, and finally uses the append () method of the jtextarea control to display the move completion information. For the event processing method of the close button, use the exit () method of the system class to exit the program. The code is as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>