import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
public class Lianxi05 {
public static void main(String[] args) throws IOException {
Lianxi05 lx = new Lianxi05();
// lx.createFiles();//
lx.upDate(“E:\Java学习”, “Android实训课程之JavaSE_”, “JavaSE”);
}
public void upDate(String src,String oldName,String newName){//传过来三个数值一个绝对路径,老名字,新名字
File file = new File(src);//文件的绝对路径
File[] f = file.listFiles();//把所有文件放到一个文件数组
int count = 0;
for (File ff : f) {
String oldname = ff.getName();//返回一个老名字
String newname = oldname.replace(oldName, newName);//替换老名字
String newPath = file.getAbsolutePath()+"\\"+newname;// 返回此抽象路径名的绝对路径名形式。
File newFile = new File(newPath);//新的文件夹需要重新实例化
boolean b = ff.renameTo(newFile);//修改成功后返回true
if(b){
count++;
}
}
System.out.println("一共有"+f.length+"个文件,修改成功"+count+"个");
}
public void createFiles() throws IOException{
File file = new File("E:\\Java学习");
if(!file.exists()){
file.mkdirs();//创建文件夹
System.out.println("文件夹创建成功");
}else{
System.out.println("文件夹创建失败");
}
System.out.println(file.getAbsolutePath());
DecimalFormat df = new DecimalFormat("000");//数字格式转换
for (int i = 1; i < 201; i++) {//文件名上后面的数字
File f = new File(file.getAbsolutePath()+"\\"+"Android实训课程之JavaSE_视频教程"+df.format(i)+".wmv");
if(!f.exists()){
f.createNewFile();//创建文件
System.out.println("文件创建成功");
}else{
System.out.println("文件创建失败");
}
}
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j <= 9; j++) {
// for (int k = 0; k <= 9; k++) {
// if(i==0 && j==0 && k==0){
// continue;
// }
// System.out.println(i+”“+j+”“+k);
// if(i==2 && j==0 && k==0){
// j=10;
// break;
// }
// }
// }
// }
}
}