黑马程序员技术交流社区
标题:
重命名400多个文件 没java哪行
[打印本页]
作者:
vermouth
时间:
2015-1-14 11:56
标题:
重命名400多个文件 没java哪行
本帖最后由 czwanglei 于 2015-1-14 13:52 编辑
:(不知道上层哪来的勇气 让我们把做过的400多个文档的命名中 所有的“-”改成“_”
于是我简单的十几分钟搞定了,不过运行时候要给同事们电脑装JRE
我觉得我还是适合代码的世界,虽然在公司只敲了那么短暂的一会儿,简单的swing,简单的file,简单的打了个jar包
我想说 程序员们 我马上就来啦 愿顺利15期 愿在黑马更上一层楼
package com.xxxxxx;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
class changeName{
public static int count = 0;
public static void main(String [] args){
File rootDir=null;
JFrame frame = new JFrame("重命名mp4文件");
frame.setSize(300,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JButton button = new JButton("打开要重命名的目录");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showOpenDialog(frame);
File rootDir = chooser.getSelectedFile();
System.out.println(rootDir.getName());
if(rootDir!=null){
iteratorFile(rootDir);
String echo = "共有"+count+"个文件重命名";
JOptionPane.showMessageDialog(frame,echo,"Finished",JOptionPane.INFORMATION_MESSAGE);
}
}
});
frame.add(button);
frame.setVisible(true);
}
public static void iteratorFile(File file){
File [] files = file.listFiles();
for(File f:files){
if (f.isDirectory()){
iteratorFile(f);
}
else {
String fileName = f.getName();
String path = f.getParent();
if(fileName.endsWith(".mp4")){
renameFile(f,path,fileName);
//System.out.println(fileName);
}
}
}
}
public static void renameFile(File f,String path,String oldName){
String newName = oldName.replace('-','_');
File newFile = new File(path,newName);
f.renameTo(newFile);
count ++;
System.out.println(newFile.getParent()+"\\"+newFile.getName()+" has changed OK");
}
}
复制代码
作者:
czwanglei
时间:
2015-1-14 13:54
获得权限,加分成功。
作者:
vermouth
时间:
2015-1-14 14:50
czwanglei 发表于 2015-1-14 13:54
获得权限,加分成功。
{:3_64:}谢谢版主 阿里嘎多
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2