1   import java.io.File;
 2   import java.util.Scanner;
 3   //批量替换文件名字符
 4   public class Test07 {
 5       public static void reName() {
 6           while (true) {
 7               Scanner sc = new Scanner(System.in);
 8               String filePath = "";// 输入路径
 9              File file = new File("");// File对象
10              String dirPath = null;// 绝对路径
11              // isDirectory() 测试此抽象路径名表示的文件是否是一个目录
12              while (!file.isDirectory()) {
13                  System.out.println("请输入你想要查看的文件夹(e:\\io)");
14                  filePath = sc.nextLine() + "\\";
15                  file = new File(filePath);
16                  // getAbsolutePath()返回此抽象路径名的绝对路径名形式。
17                  dirPath = file.getAbsolutePath();
18              }
19              System.out.println(dirPath);
20              // 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
21              File[] fileList = file.listFiles();
22              System.out.println("您输入的路径下有文件如下:");
23              for (File file2 : fileList) {//利用foreach循环体
24                  if (file2.isFile()) {//如果是文件,获得文件名,并输出
25                      String formFile = file2.getName();
26                      System.out.println(formFile);
27                  }
28              }
29              System.out.println("请输入你想要替换的旧字符:");
30              String oldStr = sc.nextLine();
31              System.out.println("请输入您想要替换的新字符:");
32              String newStr = sc.nextLine();
33              int count = 0;//声明一个int型的count,用于记录替换成功的文件数
34              for (int i = 0; i < fileList.length; i++) {
35                  String name = fileList[i].getName();//得到每个文件的文件名
36                  name = name.replace(oldStr, newStr);//替换包含oldStr字符的文件名
37                  name = dirPath + "\\" + name;//文件名
38                  File toFile = new File(name);//把name转换为File对象
39                  if (fileList[i].exists() && !toFile.exists()) {//新文件不存在才能更改
40                      fileList[i].renameTo(toFile);//rename();File的方法,用户更名
41                      count++;//替换成功文件个数+1
42                  }
43              }
44              System.out.println("替换成功,总文件数:" + fileList.length + ",成功" + count
45                      + "个,失败" + (fileList.length - count) + "个。");
46              System.out.println("失败的文件为:");
47              for (File file2 : fileList) {//输出没有替换成功的文件名
48                  if (file2.isFile()) {
49                      String formFile = file2.getName();
50                      System.out.println("\t"+formFile);
51                  }
52              }
53              sc.close();
54              System.out.println("3秒后关闭");
55              new Thread() {//新建一个线程,停留三秒后输出
56                  @Override
57                  public void run() {
58                      try {
59                          sleep(3000);
60                      } catch (InterruptedException e) {
61                          e.printStackTrace();
62                      }
63                  }
64              }.start();
65              break;
66          }
67      }
68      public static void main(String[] args) {
69          reName();
70      }
71  }
 posted on 2015-08-03 17:06  码农xk  阅读(309)  评论(0编辑  收藏  举报