package com.codeurjava.File;Execution:
import java.io.File;
public class RenameFileJava {
public static void main(String[] argv) {
Old File = new File(c:\\test\\ancienFichier.txt”);
新文件 = new File(c:\\test\\nouveauFichier.txt”);
if(old.renameTo(new))
System.out.println(文件”+old+已重命名。
else
System.out.println(失败!无法重命名文件+old+”。
}
}
文件 c:\test\ancienFichier.txt 已成功重命名。
package com.codeurjava.File;Runtime:
import java.io.File;
import java.io.IOException;
public class RenameFilesJava {
public static void main(String[] argv) 抛出 IOException {
File folder = new File(c:\\test”);
文件[] 列表 = folder.listFiles();
for (int i = 0; i < liste.length; i++) {
if (list[i].isFile()) { // 如果是文件
//获取当前文件
文件 f = new File(c:\\test\\”+list[i].getName());
字符串扩展名 = ”;
旧字符串 = list[i].getName();
//验证扩展是否为 txt
int index = old.lastIndexOf('.');
if (index > 0) {
extension = old.substring(index+1);
}
if(extension.equals(txt”)){
新字符串 = f”+i+.txt”;
f.renameTo(new File(c:\\test\\”+new));
System.out.println(old+ 重命名 =>+新);
}
else{
System.out.println(ancient+ 不是 txt 文件);
}
}
}
System.out.println(操作完成!”);
}
}
anc1.txt重命名 =>f0.txt
anc2.txt 重命名 =>f1.txt
anc3.txt 重命名为 =>f2.txt
img_01.jpg 不是 txt
table.sql 不是 txt
操作完成!
Please disable your ad blocker and refresh the window to use this website.