Chinaunix首页 | 论坛 | 博客
  • 博客访问: 490886
  • 博文数量: 23
  • 博客积分: 398
  • 博客等级: 一等列兵
  • 技术积分: 850
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 22:18
文章分类

全部博文(23)

文章存档

2013年(9)

2012年(14)

分类: Java

2013-06-16 17:27:59

       问题是这样的,有一个web项目,其中包含很多以html为后缀的文件(分别在不同目录下),如今想把他们的后缀改为 jsp,如果手工改的确比较累人,我比较喜欢偷懒,于是写个小程序帮我干,这样以后遇到此类问题就可以一劳永逸了,哈哈,是不是很爽呢。
Java代码如下(阅读将就将就吧,UC改版后插入代码功能老有问题,所以直接贴上算了。ps:):
 

参数说明:
/*
    src_dir —— 需要修改的文件所在目录
    des_dir —— 修改后保存文件所在目录
    fromSuffix —— 需要修改的文件后缀
    toSuffix —— 修改后的文件后缀
    includeChild —— 是否包含子目录(递归)
*/
    public static void rename(String src_dir, String des_dir,
            String fromSuffix, String toSuffix, boolean includeChild)
    {
    
           if(null == src_dir || null == des_dir || null == fromSuffix || null == toSuffix)
                return;
    
    try {
                if(!des_dir.endsWith("/") && !des_dir.endsWith("\\")) {//为安全起见,做下安检
                        des_dir += "/";
                        System.out.println("des_dir=" + des_dir);
                 }
                File des_directory = new File(des_dir);
            // 瞧瞧目标目录是否存在先,不存在咱就弄一个
                if(!des_directory.exists() && !des_directory.isDirectory()) {
                        //System.out.println(des_directory + " not exits or is not a directory.");
                        des_directory.mkdirs(); //(与mkdir有区别)如果当前路径中包含的父目录不存在时,也会自动根据需要创建。
                }
        

                File file = new File(src_dir);

                File[] srcFiles = file.listFiles();
                for (int i = 0; i < srcFiles.length; i++) {
                        if (srcFiles[i].isDirectory()) {
                                if(includeChild) { // 递归进入子目录
                                        String absPath = srcFiles[i].getAbsolutePath() + "/";
                                        //System.out.println("srcDir:" + absPath);
                                        rename(absPath, des_dir + absPath.substring(src_dir.length()), fromSuffix, toSuffix, includeChild);
                                }
                        } else {
                                    if (srcFiles[i].getName().endsWith(fromSuffix)) {
                                    //抽取文件名
                                            int index = srcFiles[i].getName().lastIndexOf(".");
                                            String fileName = srcFiles[i].getName().substring(0,index);
                                                //拼凑新的路径
                                                String path = des_dir + fileName + toSuffix;
                                                System.out.println("rename:" + path);
            
                                                File dest = new File(path);
                                                if(dest != null)
                                                        srcFiles[i].renameTo(dest); //源文件会被删
            
                                    }

                        }
                }
        
        } catch (Exception e) {
                e.printStackTrace();
        }

    }

阅读(3736) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~