12.9 编写程序,程序运行时输入目录名称,并把该目录下的所有文件名后缀修改为.txt。
package book;
import java.io.File;
import java.util.Scanner;
public class JiOu {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
System.out.println("请输入目录名称:");
String str = scan.next();
File file = new File("D:" + File.separator + str);
if (file.isDirectory()) {
String f[] = file.list();
for (int i = 0; i < f.length; i++) {
System.out.println(f[i]);
File f1 = new File("D:" + File.separator + str + File.separator + f[i]);
f1.renameTo(new File("D:" + File.separator + str + File.separator + f[i].split("\\.")[0] + ".txt"));
}
}
}
}
原文地址:https://blog.csdn.net/Lakers1989/article/details/78310829