批量修改文件名
来源:互联网 发布:flexfit帽子 知乎 编辑:程序博客网 时间:2024/06/17 15:29
每次写博客时需要导入一些图片时都会遇到一个困惑,就是截图的名称太长不规范,不知道哪一张是指定的图片,还有就是手机拍的照片看那个名字就是不舒服。于是写了这一个小工具用于重命名文件名。文件批量重命名工具不局限于对图片的重命名对任意文件都适用。下面是相关源代码与解析:
由于代码比较小,就没有对其作分层处理。源代码包括界面设计、文件名识别、文件重命名处理等三部分。
代码编译环境:Jdk1.6和windows xp
1. 界面设计
先来看一下界面效果:
publicRenameFiles() {
setResizable(false);
setTitle("文件批量重命名工具");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 385,435);
contentPane = new JPanel();
contentPane.setBorder(newEmptyBorder(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();
GridBagConstraints gbc_label= new GridBagConstraints();
gbc_label.fill =GridBagConstraints.VERTICAL;
gbc_label.insets = newInsets(0, 0, 5, 5);
gbc_label.gridwidth = 3;
gbc_label.gridx = 1;
gbc_label.gridy = 0;
contentPane.add(label,gbc_label);
JLabel label_10 = newJLabel();
label_10.setText("读取路径");
GridBagConstraintsgbc_label_10 = new GridBagConstraints();
gbc_label_10.anchor =GridBagConstraints.EAST;
gbc_label_10.fill =GridBagConstraints.VERTICAL;
gbc_label_10.insets = newInsets(0, 0, 5, 5);
gbc_label_10.gridx = 0;
gbc_label_10.gridy = 1;
contentPane.add(label_10,gbc_label_10);
forderField = newJTextField();
forderField.setText("请输入文件的读取路径...");
GridBagConstraintsgbc_forderField_11 = new GridBagConstraints();
gbc_forderField_11.fill =GridBagConstraints.HORIZONTAL;
gbc_forderField_11.insets =new Insets(0, 0, 5, 5);
gbc_forderField_11.gridwidth= 3;
gbc_forderField_11.gridx = 1;
gbc_forderField_11.gridy = 1;
contentPane.add(forderField,gbc_forderField_11);
JButton button = new JButton();
button.addActionListener(newActionListener() {
public voidactionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
button.setText("浏览");
GridBagConstraintsgbc_button_14 = new GridBagConstraints();
gbc_button_14.anchor =GridBagConstraints.NORTHWEST;
gbc_button_14.insets = newInsets(0, 0, 5, 0);
gbc_button_14.gridx = 4;
gbc_button_14.gridy = 1;
contentPane.add(button,gbc_button_14);
JSeparator separator_1 = newJSeparator();
GridBagConstraintsgbc_separator_600 = new GridBagConstraints();
gbc_separator_600.fill =GridBagConstraints.BOTH;
gbc_separator_600.insets =new Insets(0, 0, 5, 0);
gbc_separator_600.gridwidth =5;
gbc_separator_600.gridx = 0;
gbc_separator_600.gridy = 3;
contentPane.add(separator_1,gbc_separator_600);
JLabel label_3 = newJLabel();
label_3.setText(" 模 板:");
GridBagConstraintsgbc_label_500 = new GridBagConstraints();
gbc_label_500.anchor =GridBagConstraints.EAST;
gbc_label_500.fill = GridBagConstraints.VERTICAL;
gbc_label_500.insets = newInsets(0, 0, 5, 12);
gbc_label_500.gridx = 0;
gbc_label_500.gridy = 4;
contentPane.add(label_3,gbc_label_500);
templetField = newJTextField();
SimpleDateFormat simpledf =new SimpleDateFormat("yyyyMMdd");
String date =simpledf.format(new Date());
templetField.setText(date +"###");
GridBagConstraintsgbc_templetField_41 = new GridBagConstraints();
gbc_templetField_41.anchor =GridBagConstraints.SOUTH;
gbc_templetField_41.fill =GridBagConstraints.HORIZONTAL;
gbc_templetField_41.insets =new Insets(0, 0, 5, 5);
gbc_templetField_41.gridwidth= 3;
gbc_templetField_41.gridx =1;
gbc_templetField_41.gridy =4;
contentPane.add(templetField,gbc_templetField_41);
JLabel label_5 = newJLabel();
label_5.setText("(#指定文件位数)");
GridBagConstraintsgbc_label_504 = new GridBagConstraints();
gbc_label_504.fill =GridBagConstraints.WEST;
gbc_label_504.insets = newInsets(0, 0, 5, 0);
gbc_label_504.gridwidth = 5;
gbc_label_504.gridx = 4;
gbc_label_504.gridy = 4;
contentPane.add(label_5,gbc_label_504);
JLabel label_4 = newJLabel();
label_4.setText("开始于:");
GridBagConstraintsgbc_label_50 = new GridBagConstraints();
gbc_label_50.fill =GridBagConstraints.VERTICAL;
gbc_label_50.insets = newInsets(0, 0, 5, 5);
gbc_label_50.gridx = 0;
gbc_label_50.gridy = 5;
contentPane.add(label_4,gbc_label_50);
startSpinner = newJSpinner();
GridBagConstraintsgbc_startSpinner_51 = new GridBagConstraints();
gbc_startSpinner_51.fill =GridBagConstraints.HORIZONTAL;
gbc_startSpinner_51.insets =new Insets(0, 0, 5, 5);
gbc_startSpinner_51.gridx =1;
gbc_startSpinner_51.gridy =5;
contentPane.add(startSpinner,gbc_startSpinner_51);
JLabel label_2 = newJLabel();
label_2.setText(" 扩展名:");
GridBagConstraintsgbc_label_52 = new GridBagConstraints();
gbc_label_52.fill =GridBagConstraints.HORIZONTAL;
gbc_label_52.insets = newInsets(0, 0, 5, 5);
gbc_label_52.gridx = 2;
gbc_label_52.gridy = 5;
contentPane.add(label_2,gbc_label_52);
extNameField = newJTextField();
extNameField.setText("");
GridBagConstraintsgbc_extNameField_53 = new GridBagConstraints();
gbc_extNameField_53.fill =GridBagConstraints.HORIZONTAL;
gbc_extNameField_53.insets =new Insets(0, 0, 5, 5);
gbc_extNameField_53.gridx =3;
gbc_extNameField_53.gridy =5;
contentPane.add(extNameField,gbc_extNameField_53);
JButton startButton = newJButton();
startButton.addActionListener(newActionListener() {
public voidactionPerformed(ActionEvent e) {
do_startButton_actionPerformed(e);
}
});
startButton.setText("开始");
GridBagConstraintsgbc_startButton_54 = new GridBagConstraints();
gbc_startButton_54.anchor =GridBagConstraints.NORTH;
gbc_startButton_54.insets =new Insets(0, 0, 5, 0);
gbc_startButton_54.gridx = 4;
gbc_startButton_54.gridy = 5;
contentPane.add(startButton,gbc_startButton_54);
JSeparator separator_2 = newJSeparator();
GridBagConstraints gbc_separator_60= new GridBagConstraints();
gbc_separator_60.anchor =GridBagConstraints.NORTH;
gbc_separator_60.fill =GridBagConstraints.HORIZONTAL;
gbc_separator_60.insets = newInsets(0, 0, 5, 5);
gbc_separator_60.gridwidth =5;
gbc_separator_60.gridx = 0;
gbc_separator_60.gridy = 6;
contentPane.add(separator_2,gbc_separator_60);
JScrollPane scrollPane = newJScrollPane();
GridBagConstraintsgbc_scrollPane_70 = new GridBagConstraints();
gbc_scrollPane_70.fill =GridBagConstraints.BOTH;
gbc_scrollPane_70.gridwidth =5;
gbc_scrollPane_70.gridx = 0;
gbc_scrollPane_70.gridy = 7;
contentPane.add(scrollPane,gbc_scrollPane_70);
table = new JTable();
table.setModel(newDefaultTableModel(new Object[][] {}, new String[] { "旧文件名","新文件名" }));
scrollPane.setViewportView(table);
}
如下为工具界面,质朴了一点,呵呵。
2. 文件识别
当浏览按钮被触发后,即启动文件读取功能;并且自动识别一个文件的后缀名。
//文件读取路径
protectedvoid do_button_actionPerformed(ActionEvent e) {
JFileChooserchooser = new JFileChooser();// 创建文件选择器
//设置只选择文件夹
/**Instruction to display only directories. DIRECTORIES_ONLY=1 */
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
intoption = chooser.showOpenDialog(this);// 显示打开对话框
if(option == JFileChooser.APPROVE_OPTION) {
dir= chooser.getSelectedFile();// 获取选择的文件夹
}else {
return;
}
forderField.setText(dir+ "");// 显示文件夹信息
File[]files = dir.listFiles();
intexIndex = files[0].toString().lastIndexOf(".");
StringexName = files[0].toString().substring(exIndex + 1);
extNameField.setText(exName);
DefaultTableModelmodel = (DefaultTableModel) table.getModel();
model.setRowCount(0);
for(File file : files) {
//System.out.println(file.getName());
model.addRow(newString[] { file.getName(), null });
}
}
3. 文件重命名
触发开始按钮后处理重命名过程,对第二步中不正确的扩展名不予处理,所以不用担心文件会被处理出错。
/**
* 开始按钮的事件处理方法
*
* @param e
*/
protectedvoid do_startButton_actionPerformed(ActionEvent e) {
if(dir == null) {
JOptionPane.showMessageDialog(this,"请指定重命名路径(点击浏览)", "信息对话框", JOptionPane.WARNING_MESSAGE);
return;
}
Stringtemplet = templetField.getText();// 获取模板字符串
if(templet.isEmpty()) {
JOptionPane.showMessageDialog(this,"请确定重命名模板", "信息对话框", JOptionPane.WARNING_MESSAGE);
return;
}
//获取表格数据模型
DefaultTableModelmodel = (DefaultTableModel) table.getModel();
model.setRowCount(0);//清除表格数据
intbi = (Integer) startSpinner.getValue();// 获取起始编号
intindex = templet.indexOf("#");// 获取第一个“#”的索引
Stringcode = templet.substring(index);// 获取模板中数字占位字符串
//把模板中数字占位字符串替换为指定格式
templet= templet.replace(code, "%0" + code.length() + "d");
StringextName = extNameField.getText().toLowerCase();
//System.out.println(extName);
//if (extName.indexOf(".") == -1)
extName= "." + extName;
//获取文件中文件列表数组
File[]files = dir.listFiles(new ExtNameFileFilter(extName));
//File[] files = dir.listFiles();
//String parentFile =forderField2.getText();
//File SaveFile=new File(parentFile);
//System.out.println("aaaaaa="+SaveFile);
for(File file : files) {// 变量文件数组
//System.out.println(file.getName());
//格式化每个文件名称
Stringname = String.format(templet, bi++) + extName;
//把文件的旧名称与新名称添加到表格的数据模型
model.addRow(newString[] { file.getName(), name });
System.out.println("file="+ file);
System.out.println("filepare="+ file.getParent());
FileparentFile = file.getParentFile();// 获取文件所在文件夹对象
System.out.println("bbbbb="+ parentFile);
FilenewFile = new File(parentFile, name);
file.renameTo(newFile);//文件重命名
}
}
完整源代码链接:http://pan.baidu.com/s/1c04csgg
- 批量文件名修改
- 批量修改文件名
- 批量修改文件名
- 批量修改文件名
- 批量修改mp3文件名
- 批量修改文件名
- 批量修改文件名
- shell批量修改文件名
- Python批量修改文件名
- shell批量修改文件名
- 批量修改文件名
- 批量修改文件名
- JAVA批量修改文件名
- linux批量修改文件名
- php批量修改文件名
- shell 批量修改文件名
- 批量修改文件名
- python 批量修改文件名
- tile()函数及其在数组数值归一化中的应用
- php 拼接字符串 单引号与双引号的区别
- Java中集合的方法及子集(下)
- andriod instrumention sendKeys和sendRepeatedKeys函数
- tableview去掉多余的横线
- 批量修改文件名
- 三层学习感悟
- DSAPI多功能组件编程应用-菜单渲染
- HDFS JAVA API
- 在c中,指针数组和数组指针的表示和理解
- Android_BUG_ViewPager+Fragment(Fragment中嵌套Viewpager,Viewpager中有嵌套Fragement的场景)爆出的BUG
- memcopy()函数c语言实现和汇编实现比较
- SharePoint JS Link 之( 三)如何设置JSLink的引用
- NGUI中利用UIGrid实现下拉列表