A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

  1. package com.test21;

  2. import java.io.File;
  3. //创建方法实现:修改指定目录"c:\\test"下开头为“新建”的文件/文件夹的名字,去掉"新建"
  4. public class foldersRename {
  5.         public static void main(String[] args) {
  6.                 filesrename("c:\\test", "新建", " ");
  7.         }
  8. private static void filesrename(String s1, final String s2, String s3) {
  9.                 // TODO Auto-generated method stub
  10.                 File folder = new File(s1);
  11.                 if (folder != null && folder.exists()) {
  12.                         File[] lf = folder.listFiles();
  13.                         for (File f1 : lf) {
  14.                                 if (f1.isFile() && f1.getName().startsWith(s2)) {
  15.                                         String newname = f1.getName().replace(s2, s3);
  16.                                         File newfile = new File(s1, newname);
  17.                                         folder.renameTo(newfile);
  18.                                 }

  19.                         }
  20.                 }
  21.         }
  22. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马