1.先遍历给定目录
2.判断 listFile()返回值是目录还是文件,若是文件就getname然后用rename方法改名。下面是我写的在我的电脑是可以的
- import java.io.*;
- import java.util.Scanner;
- public class Test
- {
- public static void main(String[] args)
- {
- String string = null;
- Scanner scanner = new Scanner(System.in);
- string =scanner.next();
- File dirFile =new File(string);
- rename(dirFile);
- System.out.print("Done");
- }
- public static void rename(File dir)
- {
- File[] file = dir.listFiles();
- File file2 =null;
- String filename = null;
- String filetype = ".txt";
- for (int i = 0; i <file.length; i++)
- {
- file2 = file[i];
- if (!(file2.isDirectory()))
- {
- filename = file2.getName();
- filename = file[i].toString().substring(0,file[i].toString().lastIndexOf("."));
- filename = filename+filetype;
- file2.renameTo(new File(filename));
- }
- }
- }
- }
复制代码 |