Java allows you to quickly move files in batches

Source: Internet
Author: User

File movement is a common operation in computer resource management, which can be achieved through file cutting, copying, or dragging with the mouse in the operating system. However

Train of Thought Analysis:

The Code is as follows:

Import java. awt. eventQueue; public class QuickMoveFiles extends JFrame {/***/private static final long serialVersionUID =-inline; 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"); setdefaclocloseoperation (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 (); iterator = 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 (constraint); JLabel label = new JLabel ("\ u9009 \ u62E9 \ u6E90 \ u6587 \ u4EF6 \ uFF1A"); export gbc_label = new GridBagConstraints (); constraint = constraint. 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 (); iterator = 3; iterator = 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 actionreceivmed (ActionEvent e) {items (e) ;}}); GridBagConstraints gbc_browserButton1 = new GridBagConstraints (); gbc_browserButton1.insets = new Insets (0, 0, 5, 0); counts = 4; counts = 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 (); counts = 3; counts = 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 actionreceivmed (ActionEvent e) {items (e) ;}}); GridBagConstraints gbc_browserButton2 = new GridBagConstraints (); gbc_browserButton2.insets = new Insets (0, 0, 5, 0); counts = 4; counts = 1; contentPane. add (browserButton2, gbc_browserButton2); JLabel label_2 = new JLabel ("\ u64CD \ u4F5C \ u8BB0 \ u5F55 \ uFF1A"); gbc_label_2 = new second (); Second = second. 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 outer (); outer = 4; outer = 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 actionreceivmed (ActionEvent e) {response (e) ;}}); GridBagConstraints gbc_moveButton = new GridBagConstraints (); constraint = 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 actionreceivmed (ActionEvent e) {response (e) ;}}); GridBagConstraints gbc_closeButton = new GridBagConstraints (); constraint = 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_browserbutton?action=med (ActionEvent e) {JFileChooser chooser = new JFileChooser (); // create a file selector chooser. setMultiSelectionEnabled (true); // set the int option = chooser for multiple files. showOpenDialog (this); // display the file opening dialog box if (option = JFileChooser. APPROVE_OPTION) {files = chooser. getSelectedFiles (); // obtain the selected file array sourceFolderField. setText (""); // clear the text box StringBuilder filesStr = new StringBuilder (); for (File file: files) {// traverse the File array filesStr. append ("," + file. getName (); // name of the connection file} String str = filesStr. substring (1); // obtain the sourceFolderField string of all file names. setText (str); // set the File name to the text box} else {files = new File [0]; sourceFolderField. setText (""); // clear the text box}/*** select the Browse button for the target folder ** @ param e */protected void do_browserbutton2_actionreceivmed (ActionEvent e) {JFileChooser chooser = new JFileChooser (); // create a file selector // set the selector to take effect only for folders. setFileSelectionMode (JFileChooser. DIRECTORIES_ONLY); int option = chooser. showOpenDialog (this); // display the file opening dialog box if (option = JFileChooser. APPROVE_OPTION) {dir = chooser. getSelectedFile (); // get the selected folder targetFolderField. setText (dir. toString (); // display the folder to the text box} else {dir = null; targetFolderField. setText ("") ;}}/*** event handling method for the close button ** @ param e */protected void do_closebutton_actionreceivmed (ActionEvent e) {System. exit (0);}/*** how to handle the event of the move button ** @ param e */protected void do_movebutton_actionreceivmed (ActionEvent e) {if (files. length <= 0 | dir = null) // determines whether the File array has any element return; for (file File: files) {// traverse the File array File newFile = new file (dir, File. getName (); // create the infoArea object to be moved. append (file. getName () + "\ t move to \ t" + dir); // display the move record file. renameTo (newFile); // move the infoArea file. append ("------ completed \ n"); // display the mobile information} // display the infoArea operation completed. append ("################## operation completed ################# ##\ n ");}}

Effect

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.