<pre name="code" class="java">
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) throws IOException {
Test test = new Test();
test.createFiles();
test.upDate("E:\\Java学习", "科技Android实训课程之JavaSE培训_", "JavaSE");
}
// 成员方法
public void upDate(String src, String oldName, String newName) {
File file = new File(src);
File[] f = file.listFiles();
for (File ff : f) {
String older = ff.getName();
String newer = older.<span style="color:#ff0000;">replace</span>(oldName, newName);// String的replace方法
String newPath = file.getAbsolutePath() + "\\" + newer;
File newFile = new File(newPath);
boolean b = ff.renameTo(newFile);// 重新命名此抽象路径名表示的文件
}
System.out.println("修改成功");
}
// 成员方法
public void createFiles() throws IOException {
File file = new File("E:\\Java学习");
if (!file.exists()) {// 创建文件夹
file.mkdirs();
} else {
}
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();// 当且仅当不存在具指定名称的文件时,创建一个新的空文件。
} else {
}
}
// 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;
// }
// }
// }
// }//也可以得到000—200;
}
}
创建空文件并重命名
最新推荐文章于 2024-02-21 20:46:07 发布