The implementation method of fast batch moving files in Java _java

Source: Internet
Author: User

File movement is a common operation of computer resource management, which can be achieved by cutting and copying files or dragging the mouse in the operating system. However, in the Java file programming implementation, mostly by copying files to the destination, and then delete all files to achieve. This is not a big disadvantage for small files, but if you move a few large files, it slows the operation and wastes system resources. This example uses the Renameto () method of the file class to directly implement fast file movement, even if a few gigabytes of files do not have to wait too long.

Thinking Analysis:

The first is the view layer. Here's a suggestion, because in some control events, other controls are often accessed, and the control's event method uses almost the same parameters as a fixed ActionEvent class, rarely passing other arguments. So even though the view is generated automatically with a drag control, you want to set these controls as member variables of the class in your code. In this instance, to use the JPanel control as a container for other controls, the JLabel control is used to display fixed information, the JTextField control is used to display the files and destination folders to be moved, and the JButton control is used to select the source folder, the destination folder, and to implement the move and shutdown programs. JScrollPane is used to display the bars, and the JTextArea controls are used to display the action records.
Then the model layer. For the browse button, to get the file name array and the path to the destination folder in the source folder, you need to define a file-type array member variable to save the file name, and then define a file member variable to save the target path.
When you select the Browse button for the source file, first create a JFileChooser file selector, using the setmultiselectionenabled (true) of the JFileChooser class, and the method settings to select more than one. by Showopendialog (this) of the JFileChooser class, the File Selection dialog box is displayed, and if the user confirms that the JFileChooser class is used Getselectedfiles () Method gets the selected array of files and assigns values to the previously defined file-type array member variables, empties the text box with the SetText ("") Method of the JTextField () class to remove the record from the previous operation, creates a new StringBuilder object, and uses the foreach () Loops through the file array, connecting the file name through the Append () method of the StringBuilder class, because there is a "," in the front, and then using the substring () method of the StringBuilder class to get a string of all the file names. The file name string is displayed to the text box by the SetText () method of the Jtextfieldl class.
For the browse button that selects the destination folder, first create a JFileChooser file selector, and use the Setfileselectionmode () method of the JFileChooser class to set the selector to only take effect on the folder. The Showopendialog () method of the JFileChooser class displays the File Open dialog box, using the Getselectedfile () method of the JFileChooser class to get the selected folder. Finally, use the SetText () method of the JTextField control to display the folder to the text box.
For the event handling method of the Move button, first use the length property of the array to determine if there are elements in the file array, and if so, use the foreach () loop to iterate through the file array and create a moving target file for each file element in the group. The move record is displayed using the Append () method of the JTextArea control, the Renameto () method of the file class is used to implement file movement, and the JTextArea control's append () method is used to display the move completion information.
For the event handling method of the Close button, exit the program using the exit () method of the System class.
The code is as follows:

Copy Code code as follows:

Import Java.awt.EventQueue;

public class Quickmovefiles extends JFrame {

/**
*
*/
Private static final long serialversionuid = -666045931923008374l;
Private JPanel ContentPane;
Private JTextArea Infoarea;
Private JTextField Sourcefolderfield;
Private JTextField Targetfolderfield;
Private file[] files;
Private File dir;

/**
* Launch the application.
*/
public static void Main (string[] args) {
Eventqueue.invokelater (New Runnable () {
public void Run () {
try {
Quickmovefiles frame = new Quickmovefiles ();
Frame.setvisible (TRUE);
catch (Exception e) {
E.printstacktrace ();
}
}
});
}

/**
* Create the frame.
*/
Public Quickmovefiles () {
Settitle ("\u5feb\u901f\u6279\u91cf\u79fb\u52a8\u6587\u4ef6");
Setdefaultcloseoperation (Jframe.exit_on_close);
SetBounds (100, 100, 507, 299);
ContentPane = new JPanel ();
Contentpane.setborder (New Emptyborder (5, 5, 5, 5));
Setcontentpane (ContentPane);
GridBagLayout Gbl_contentpane = new GridBagLayout ();
Gbl_contentpane.columnwidths = new int[] {0, 178, 0, 0, 0, 0};
Gbl_contentpane.rowheights = new int[] {0, 0, 169, 0, 0};
Gbl_contentpane.columnweights = new double[] {0.0, 1.0, 0.0, 0.0, 0.0,
Double.min_value};
Gbl_contentpane.rowweights = new double[] {0.0, 0.0, 1.0, 0.0,
Double.min_value};
Contentpane.setlayout (Gbl_contentpane);

JLabel label = new JLabel ("\u9009\u62e9\u6e90\u6587\u4ef6\uff1a");
Gridbagconstraints Gbc_label = new Gridbagconstraints ();
Gbc_label.anchor = Gridbagconstraints.east;
Gbc_label.insets = new Insets (0, 0, 5, 5);
Gbc_label.gridx = 0;
Gbc_label.gridy = 0;
Contentpane.add (label, Gbc_label);

Sourcefolderfield = new JTextField ();
Gridbagconstraints Gbc_sourcefolderfield = new Gridbagconstraints ();
Gbc_sourcefolderfield.gridwidth = 3;
Gbc_sourcefolderfield.insets = new Insets (0, 0, 5, 5);
Gbc_sourcefolderfield.fill = Gridbagconstraints.horizontal;
Gbc_sourcefolderfield.gridx = 1;
Gbc_sourcefolderfield.gridy = 0;
Contentpane.add (Sourcefolderfield, Gbc_sourcefolderfield);
Sourcefolderfield.setcolumns (10);

JButton browserButton1 = new JButton ("\u6d4f\u89c8");
Browserbutton1.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Do_browserbutton1_actionperformed (e);
}
});
Gridbagconstraints gbc_browserbutton1 = new Gridbagconstraints ();
Gbc_browserbutton1.insets = new Insets (0, 0, 5, 0);
Gbc_browserbutton1.gridx = 4;
Gbc_browserbutton1.gridy = 0;
Contentpane.add (BrowserButton1, Gbc_browserbutton1);

JLabel label_1 = new JLabel (
"\U9009\U62E9\U76EE\U6807\U6587\U4EF6\U5939\UFF1A");
Gridbagconstraints gbc_label_1 = new Gridbagconstraints ();
Gbc_label_1.anchor = Gridbagconstraints.east;
Gbc_label_1.insets = new Insets (0, 0, 5, 5);
Gbc_label_1.gridx = 0;
Gbc_label_1.gridy = 1;
Contentpane.add (Label_1, Gbc_label_1);

Targetfolderfield = new JTextField ();
Gridbagconstraints Gbc_targetfolderfield = new Gridbagconstraints ();
Gbc_targetfolderfield.gridwidth = 3;
Gbc_targetfolderfield.insets = new Insets (0, 0, 5, 5);
Gbc_targetfolderfield.fill = Gridbagconstraints.horizontal;
Gbc_targetfolderfield.gridx = 1;
Gbc_targetfolderfield.gridy = 1;
Contentpane.add (Targetfolderfield, Gbc_targetfolderfield);
Targetfolderfield.setcolumns (10);

JButton browserButton2 = new JButton ("\u6d4f\u89c8");
Browserbutton2.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Do_browserbutton2_actionperformed (e);
}
});
Gridbagconstraints gbc_browserbutton2 = new Gridbagconstraints ();
Gbc_browserbutton2.insets = new Insets (0, 0, 5, 0);
Gbc_browserbutton2.gridx = 4;
Gbc_browserbutton2.gridy = 1;
Contentpane.add (BrowserButton2, Gbc_browserbutton2);

JLabel label_2 = new JLabel ("\u64cd\u4f5c\u8bb0\u5f55\uff1a");
Gridbagconstraints gbc_label_2 = new Gridbagconstraints ();
Gbc_label_2.anchor = Gridbagconstraints.east;
Gbc_label_2.insets = new Insets (0, 0, 5, 5);
Gbc_label_2.gridx = 0;
Gbc_label_2.gridy = 2;
Contentpane.add (label_2, gbc_label_2);

JScrollPane ScrollPane = new JScrollPane ();
Gridbagconstraints Gbc_scrollpane = new Gridbagconstraints ();
Gbc_scrollpane.gridwidth = 4;
Gbc_scrollpane.insets = new Insets (0, 0, 5, 0);
Gbc_scrollpane.fill = Gridbagconstraints.both;
Gbc_scrollpane.gridx = 1;
Gbc_scrollpane.gridy = 2;
Contentpane.add (ScrollPane, Gbc_scrollpane);

Infoarea = new JTextArea ();
Scrollpane.setviewportview (Infoarea);

JButton Movebutton = new JButton ("\u79fb\u52a8");
Movebutton.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Do_movebutton_actionperformed (e);
}
});
Gridbagconstraints Gbc_movebutton = new Gridbagconstraints ();
Gbc_movebutton.insets = new Insets (0, 0, 0, 5);
Gbc_movebutton.gridx = 1;
Gbc_movebutton.gridy = 3;
Contentpane.add (Movebutton, Gbc_movebutton);

JButton CloseButton = new JButton ("\u5173\u95ed");
Closebutton.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Do_closebutton_actionperformed (e);
}
});
Gridbagconstraints Gbc_closebutton = new Gridbagconstraints ();
Gbc_closebutton.insets = new Insets (0, 0, 0, 5);
Gbc_closebutton.gridx = 2;
Gbc_closebutton.gridy = 3;
Contentpane.add (CloseButton, Gbc_closebutton);
}

/**
* Select the browse button for the source file
*
* @param E
*/
protected void do_browserbutton1_actionperformed (ActionEvent e) {
JFileChooser chooser = new JFileChooser ();//Create File Selector
Chooser.setmultiselectionenabled (TRUE);//Set File multiple selections
int option = Chooser.showopendialog (this);//Show File Open dialog box
if (option = = jfilechooser.approve_option) {
Files = Chooser.getselectedfiles ();//Get the selected file array
Sourcefolderfield.settext ("");/Empty text box
StringBuilder filesstr = new StringBuilder ();
for (file file:files) {//Traverse file array
Filesstr.append ("," + file.getname ());//Connection file name
}
String str = filesstr.substring (1);//Get strings for all file names
Sourcefolderfield.settext (str);//Set file name information to text box
} else {
Files = new File[0];
Sourcefolderfield.settext ("");/Empty text box
}
}

/**
* Select the browse button for the destination folder
*
* @param E
*/
protected void do_browserbutton2_actionperformed (ActionEvent e) {
JFileChooser chooser = new JFileChooser ();//Create File Selector
The settings selector only takes effect for folders
Chooser.setfileselectionmode (jfilechooser.directories_only);
int option = Chooser.showopendialog (this);//Show File Open dialog box
if (option = = jfilechooser.approve_option) {
dir = Chooser.getselectedfile ();//Get the selected folder
Targetfolderfield.settext (Dir.tostring ());//Show folder to text box
} else {
dir = null;
Targetfolderfield.settext ("");
}
}

/**
* Turn off the event handling method for the button
*
* @param E
*/
protected void do_closebutton_actionperformed (ActionEvent e) {
System.exit (0);
}

/**
* Event handling method for moving buttons
*
* @param E
*/
protected void do_movebutton_actionperformed (ActionEvent e) {
if (files.length <= 0 | | | dir = NULL)//Determine whether an array of files has elements
Return
for (file file:files) {//Traverse file array
File NewFile = new file (dir, File.getname ());//Create move target file
Infoarea.append (File.getname () + "\ t move to \ t" + dir);//Show Move record
File.renameto (newFile);//File move
Infoarea.append ("------\ n");//Show Move completion information
}
Show Operation complete
Infoarea.append ("################# #操作完成 ###################\n");
}
}

Effect as shown:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.