Java implementation file Batch rename concrete instance _java

Source: Internet
Author: User
Tags gettext numeric save file

The Windows operating system can implement renaming file operations, but it cannot implement bulk renaming. This example implements the batch renaming function, which can be renamed in batches of the same type in a folder according to certain rules. The user can give the renamed template, and the program can rename the corresponding file according to the template. In addition, you can add special symbols to the renamed template, and the program replaces these special symbols with the renamed file number.

Thinking Analysis:

1. Look at the view layer first, you need some JLabel controls to display the information that indicates the user, three JTextField controls display the selected path, the input filename template is entered as the extension, and two JButton controls are used to browse folders and begin renaming respectively. A Jseparator control represents a split line, a Jspinner control represents a start number, a JScrollPane control as a container, where a jtable control is placed to list the old file name and the new file name.


2. Look at the model layer again. First define the event-handling method for the Browse button, create a JFileChooser file selector in the method, and use the Setfileselectionmode () method of the JFileChooser class to select only the folder. The Showopendialog () display opens the dialog box through the JFileChooser class, and if the user clicks on the confirmation button, the JFileChooser class's Getselectedfile () method is used to get the selected folder. Finally, the folder information is displayed using the SetText () method of the JTextField control.


3. Define a class to implement the FileFilter interface, save the file name extension in the constructor of the class, and then define a method in which the String class's EndsWith () method is used to filter the file name extension.


4. Then define the event handling method for the Start button, first use the GetText () method of the JTextField control to get the template string, and if it is null, prompt the user to enter the template through the Showmessagedialog () method of the Joptionpane class. The DefaultTableModel object is then created and the table data model is obtained using the Getmodel () method of the JTable class, with the Setrowcount (0) of the JTable class, the table data is cleared, and the Jspinner class GetValue () is used. Method gets the starting number, uses the IndexOf method of the string class to get the index of the first "#", and uses the substring () method of the string class to get the numeric placeholder string in the template, Use the Replace () method of the string class to replace the numeric placeholder string in the template with the specified format, in order to standardize the use of the String class's toLowerCase () method to convert the user-entered extension to lowercase, if the user does not enter "." Then the file list array in the folder is fetched using the Listfiles () method of the file class, looping through each file using a foreach () loop, and formatting each file name with the format () method of the String class. Use the AddRow () method of the DefaultTableModel class to add the old name and new name of the file to the table's data model, and use the Getparentfile () method of the file class to get the folder object that contains the destination file. Creates a file object and initializes it to a new filename, and finally uses the Renameto () method of the file class to implement a filename rename.


The code is as follows:

Copy Code code as follows:



Import Java.awt.EventQueue;


Import java.awt.event.ActionEvent;


Import Java.awt.event.ActionListener;


Import Java.io.File;


Import Java.io.FileFilter;





Import Javax.swing.JButton;


Import Javax.swing.JFileChooser;


Import Javax.swing.JFrame;


Import Javax.swing.JLabel;


Import Javax.swing.JOptionPane;


Import Javax.swing.JPanel;


Import Javax.swing.JScrollPane;


Import Javax.swing.JSeparator;


Import Javax.swing.JSpinner;


Import javax.swing.JTable;


Import Javax.swing.JTextField;


Import Javax.swing.border.EmptyBorder;


Import Javax.swing.table.DefaultTableModel;


Import Java.awt.GridBagLayout;


Import java.awt.GridBagConstraints;


Import Java.awt.Insets;





/**


* Get filter for file list


*


* @author Li Zhongji


*/


public class Renamefiles extends JFrame {





/**


*


*/


Private static final long serialversionuid = 4534371106024773867L;





Private Final class Extnamefilefilter implements FileFilter {


Private String Extname;





Public Extnamefilefilter (String extname) {


This.extname = extname;//Save file name extension


}





@Override


Public Boolean accept (File pathname) {


Filter file name extension


if (Pathname.getname (). toUpperCase ()


. EndsWith (Extname.touppercase ()))


return true;


return false;


}


}





Private JPanel ContentPane;


Private JTextField Forderfield;


Private JTextField Templetfield;


Private File dir;


Private JTable table;


Private JTextField Extnamefield;


Private JSpinner Startspinner;





/**


* Launch the application.


*/


public static void Main (string[] args) {


Eventqueue.invokelater (New Runnable () {


public void Run () {


try {


Renamefiles frame = new Renamefiles ();


Frame.setvisible (TRUE);


catch (Exception e) {


E.printstacktrace ();


}


}


});


}





/**


* Create the frame.


*/


Public Renamefiles () {


Setresizable (FALSE);


Settitle ("File batch renaming");


Setdefaultcloseoperation (Jframe.exit_on_close);


SetBounds (100, 100, 383, 409);


ContentPane = new JPanel ();


Contentpane.setborder (New Emptyborder (5, 5, 5, 5));


Setcontentpane (ContentPane);


GridBagLayout Gbl_contentpane = new GridBagLayout ();


Gbl_contentpane.columnwidths = new int[] {72, 54, 60, 87, 91, 0};


Gbl_contentpane.rowheights = new int[] {25, 25, 10, 25, 24, 25, 2,


216, 0};


Gbl_contentpane.columnweights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0,


Double.min_value};


Gbl_contentpane.rowweights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0,


0.0, 0.0, 0.0, double.min_value};


Contentpane.setlayout (Gbl_contentpane);





JLabel label = new JLabel ();


Label.settext ("File Batch renaming module:");


Gridbagconstraints Gbc_label = new Gridbagconstraints ();


Gbc_label.fill = gridbagconstraints.vertical;


Gbc_label.insets = new Insets (0, 0, 5, 5);


Gbc_label.gridwidth = 3;


Gbc_label.gridx = 1;


Gbc_label.gridy = 0;


Contentpane.add (label, Gbc_label);





JLabel label_1 = new JLabel ();


Label_1.settext ("File path:");


Gridbagconstraints gbc_label_1 = new Gridbagconstraints ();


Gbc_label_1.anchor = Gridbagconstraints.east;


Gbc_label_1.fill = gridbagconstraints.vertical;


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);





Forderfield = new JTextField ();


Forderfield.settext ("");


Gridbagconstraints Gbc_forderfield = new Gridbagconstraints ();


Gbc_forderfield.fill = Gridbagconstraints.horizontal;


Gbc_forderfield.insets = new Insets (0, 0, 5, 5);


Gbc_forderfield.gridwidth = 3;


Gbc_forderfield.gridx = 1;


Gbc_forderfield.gridy = 1;


Contentpane.add (Forderfield, Gbc_forderfield);





JButton button = new JButton ();


Button.addactionlistener (new ActionListener () {


public void actionperformed (ActionEvent e) {


Do_button_actionperformed (e);


}


});


Button.settext ("browse");


Gridbagconstraints Gbc_button = new Gridbagconstraints ();


Gbc_button.anchor = Gridbagconstraints.northwest;


Gbc_button.insets = new Insets (0, 0, 5, 0);


Gbc_button.gridx = 4;


Gbc_button.gridy = 1;


Contentpane.add (button, Gbc_button);





Jseparator separator_1 = new Jseparator ();


Gridbagconstraints gbc_separator_1 = new Gridbagconstraints ();


Gbc_separator_1.fill = Gridbagconstraints.both;


Gbc_separator_1.insets = new Insets (0, 0, 5, 0);


Gbc_separator_1.gridwidth = 5;


Gbc_separator_1.gridx = 0;


Gbc_separator_1.gridy = 2;


Contentpane.add (separator_1, gbc_separator_1);





JLabel label_5 = new JLabel ();


Label_5.settext ("Use # to specify the position of the number count, use * to insert the original filename:");


Gridbagconstraints gbc_label_5 = new Gridbagconstraints ();


Gbc_label_5.fill = gridbagconstraints.vertical;


Gbc_label_5.insets = new Insets (0, 0, 5, 0);


Gbc_label_5.gridwidth = 5;


Gbc_label_5.gridx = 0;


Gbc_label_5.gridy = 3;


Contentpane.add (Label_5, gbc_label_5);





JLabel label_3 = new JLabel ();


Label_3.settext ("Template:");


Gridbagconstraints gbc_label_3 = new Gridbagconstraints ();


Gbc_label_3.anchor = Gridbagconstraints.east;


Gbc_label_3.fill = gridbagconstraints.vertical;


Gbc_label_3.insets = new Insets (0, 0, 5, 5);


Gbc_label_3.gridx = 0;


Gbc_label_3.gridy = 4;


Contentpane.add (Label_3, gbc_label_3);





Templetfield = new JTextField ();


Templetfield.settext ("catrestaurant###");


Gridbagconstraints Gbc_templetfield = new Gridbagconstraints ();


Gbc_templetfield.anchor = Gridbagconstraints.south;


Gbc_templetfield.fill = Gridbagconstraints.horizontal;


Gbc_templetfield.insets = new Insets (0, 0, 5, 5);


Gbc_templetfield.gridwidth = 3;


Gbc_templetfield.gridx = 1;


Gbc_templetfield.gridy = 4;


Contentpane.add (Templetfield, Gbc_templetfield);





JLabel label_4 = new JLabel ();


Label_4.settext ("Beginning with:");


Gridbagconstraints gbc_label_4 = new Gridbagconstraints ();


Gbc_label_4.fill = gridbagconstraints.vertical;


Gbc_label_4.insets = new Insets (0, 0, 5, 5);


Gbc_label_4.gridx = 0;


Gbc_label_4.gridy = 5;


Contentpane.add (Label_4, gbc_label_4);





Startspinner = new JSpinner ();


Gridbagconstraints Gbc_startspinner = new Gridbagconstraints ();


Gbc_startspinner.fill = Gridbagconstraints.horizontal;


Gbc_startspinner.insets = new Insets (0, 0, 5, 5);


Gbc_startspinner.gridx = 1;


Gbc_startspinner.gridy = 5;


Contentpane.add (Startspinner, Gbc_startspinner);





JLabel label_2 = new JLabel ();


Label_2.settext ("Extended Name:");


Gridbagconstraints gbc_label_2 = new Gridbagconstraints ();


Gbc_label_2.fill = Gridbagconstraints.horizontal;


Gbc_label_2.insets = new Insets (0, 0, 5, 5);


Gbc_label_2.gridx = 2;


Gbc_label_2.gridy = 5;


Contentpane.add (label_2, gbc_label_2);





JButton Startbutton = new JButton ();


Startbutton.addactionlistener (new ActionListener () {


public void actionperformed (ActionEvent e) {


Do_startbutton_actionperformed (e);


}


});





Extnamefield = new JTextField ();


Extnamefield.settext ("JPG");


Gridbagconstraints Gbc_extnamefield = new Gridbagconstraints ();


Gbc_extnamefield.fill = Gridbagconstraints.horizontal;


Gbc_extnamefield.insets = new Insets (0, 0, 5, 5);


Gbc_extnamefield.gridx = 3;


Gbc_extnamefield.gridy = 5;


Contentpane.add (Extnamefield, Gbc_extnamefield);


Startbutton.settext ("Start");


Gridbagconstraints Gbc_startbutton = new Gridbagconstraints ();


Gbc_startbutton.anchor = Gridbagconstraints.north;


Gbc_startbutton.insets = new Insets (0, 0, 5, 0);


Gbc_startbutton.gridx = 4;


Gbc_startbutton.gridy = 5;


Contentpane.add (Startbutton, Gbc_startbutton);





Jseparator separator_2 = new Jseparator ();


Gridbagconstraints gbc_separator_2 = new Gridbagconstraints ();


Gbc_separator_2.anchor = Gridbagconstraints.north;


Gbc_separator_2.fill = Gridbagconstraints.horizontal;


Gbc_separator_2.insets = new Insets (0, 0, 5, 0);


Gbc_separator_2.gridwidth = 5;


Gbc_separator_2.gridx = 0;


Gbc_separator_2.gridy = 6;


Contentpane.add (separator_2, gbc_separator_2);





JScrollPane ScrollPane = new JScrollPane ();


Gridbagconstraints Gbc_scrollpane = new Gridbagconstraints ();


Gbc_scrollpane.fill = Gridbagconstraints.both;


Gbc_scrollpane.gridwidth = 5;


Gbc_scrollpane.gridx = 0;


Gbc_scrollpane.gridy = 7;


Contentpane.add (ScrollPane, Gbc_scrollpane);





Table = new JTable ();


Table.setmodel (New DefaultTableModel (new object[][] {}, new string[] {


"Old file name", "New FileName"});


Scrollpane.setviewportview (table);


}





/**


* Browse button Event handling method


*


* @param E


*/


protected void do_button_actionperformed (ActionEvent e) {


JFileChooser chooser = new JFileChooser ();//Create File Selector


Set Select Folder only


Chooser.setfileselectionmode (jfilechooser.directories_only);


int option = Chooser.showopendialog (this);//Show Open dialog box


if (option = = jfilechooser.approve_option) {


dir = Chooser.getselectedfile ();//Get the selected folder


} else {


dir = null;


}


Forderfield.settext (dir + "")//Display folder information


}





/**


* Event handling method for Start button


*


* @param E


*/


protected void do_startbutton_actionperformed (ActionEvent e) {


String templet = Templetfield.gettext ();//Get template string


if (Templet.isempty ()) {


Joptionpane.showmessagedialog (This, "Make sure to rename the template", "Information dialog box",


Joptionpane.warning_message);


Return


}


Get the Tabular Data model


DefaultTableModel model = (DefaultTableModel) Table.getmodel ();


Model.setrowcount (0);//Clear table data


int bi = (Integer) startspinner.getvalue ()//Get Start number


int index = Templet.indexof ("#");//Get the index of the first "#"


String code = templet.substring (index);//Get the number placeholder string in the template


Replaces the number placeholder string in the template with the specified format


Templet = templet.replace (Code, "%0" + code.length () + "D");


String extname = Extnamefield.gettext (). toLowerCase ();


if (Extname.indexof (".") = = 1)


Extname = "." + Extname;


Get an array of file lists in a file


file[] files = dir.listfiles (new Extnamefilefilter (Extname));


for (file file:files) {//variable file array


Format each file name


String name = String.Format (Templet, bi++) + extname;


Add the old name of the file to the data model of the table with the new name


Model.addrow (new string[] {file.getname (), name});


File Parentfile = File.getparentfile ();//Get File Folder object


File NewFile = new file (parentfile, name);


File.renameto (newFile);//File Rename


}


}


}


Effect as shown:

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.