首页 > 学院 > 开发设计 > 正文

批量替换文件字符

2019-11-15 01:02:02
字体:
来源:转载
供稿:网友
批量替换文件字符
 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++;//替换成功文件个数+142                  }43              }44              System.out.println("替换成功,总文件数:" + fileList.length + ",成功" + count45                      + "个,失败" + (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                  @Override57                  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  }

上一篇:UEditor FTP上传

下一篇:newFixedThreadPool

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表