在做一个测试工具.在之中遇到了一个问题!
描叙:
1.用Java递归来修改文件夹及其下面所有文件的名字.
2.感兴趣的可以帮我解决一下另一个问题
  (指定某个文件中的String用其他的String来代替.)
如:
  
      将 E:\oracle\admin\oracle
      改 E:\oracle_1\admin_1\oracle_1 
  

解决方案 »

  1.   

    public class Test {
    public static void main(String[] args) throws Exception {
    changeFile(new java.io.File("d:/test"));
    } static void changeFile(java.io.File f) throws Exception {
    if (f.isFile()) {
    f.renameTo(new java.io.File(f.getPath() + "aaa"));
    } else {
    java.io.File[] fs = f.listFiles();
    for (java.io.File f3 : fs)
    changeFile(f3);
    f.renameTo(new java.io.File(f.getPath() + "aaa"));
    }
    }
    }
    文件名处自己改下递归的原理是这样的
      

  2.   


    //另外异常我是抛出的,你自己处理下
    public class Test {
    public static void main(String[] args) throws Exception {
    changeFile(new java.io.File("d:/test"));
    } static void changeFile(java.io.File f) throws Exception {
    if (f.isFile()) {//如果是文件,直接更名
    f.renameTo(new java.io.File(f.getPath() + "aaa"));
    } else {//如果是文件夹,
    java.io.File[] fs = f.listFiles();//得到文件夹下的所有文件列表。
    for (java.io.File f3 : fs)//foreach循环,取文件
    changeFile(f3);//递归
    f.renameTo(new java.io.File(f.getPath() + "aaa"));//循环完后,把该目录更名。
    }
    }
    }
      

  3.   

    okay.Your think all are right.But you have not right at all for your codes which are (f.getPath() + "aaa").
    The code change also the file suffix.
    Thank for your help and have a good time!
      

  4.   

    It's would be great if which one can solve the second problem i said above.
    Thanks a lot and have a good mood every day!
      

  5.   

    兄弟.我这里写了一些代码:
    但是出现了问题.帮看看吧:
    目录结构是:
    e:\test
    e:\test\test
    e:\test\test\A.txt
    e:\test\test\test
    e:\test\test\test\test.txt
    Java Code:    public static void main(String[] args) throws Exception {
            changeFile(new java.io.File("e:/test"));
        }    static void changeFile(java.io.File f) throws Exception {
         String filePath = f.getAbsolutePath();
         System.out.println(filePath);
    int index = filePath.indexOf(f.getName());
    filePath = filePath.substring(0, index) + regexReplace(f.getName(),"ok","test");
            if (f.isFile()) {//如果是文件,直接更名
                f.renameTo(new java.io.File(filePath));
            } else {//如果是文件夹,
                java.io.File[] fs = f.listFiles();//得到文件夹下的所有文件列表。
                for (int i=0;i<fs.length;i++)//foreach循环,取文件
                    changeFile(fs[i]);//递归
                f.renameTo(new java.io.File(filePath));//循环完后,把该目录更名。
            }
        }
    public static String regexReplace(String str, String replacement, String regex) {
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(str);
    if (m.find()) {
    str = m.replaceAll(replacement);
    }
    return str;
    }
      

  6.   

    filePath = filePath.substring(0, index) + regexReplace(f.getName(),"ok","test");
    你这里传的是指定的替换达。只替换名字中包涵有test的文件名。你试着把你的文件夹下的其它文件名改一些包涵有test的文件名。你就会发现效果了。
      

  7.   

    package com.linkage.reflect;
    import java.io.File;
    import java.util.regex.Pattern;
    public class FileRename{
    /*public void ChangeName(java.io.File f){
        String filePath=f.getAbsolutePath();
        System.out.println(filePath);
        int index=filePath.indexOf(f.getName());
        filePath=filePath.substring(0,index)+regexReplace(f.getName(),"ok","");
        if(f.isFile()){
         f.renameTo(new java.io.File(filePath));
        }
    }*/
    public static void regexReplace(File f){
    String filePath=f.getAbsolutePath();
        System.out.println(filePath.substring(0,filePath.indexOf(f.getName())));
    filePath=filePath.substring(0,filePath.indexOf(f.getName()))+regexReplace(f.getName(),
    f.getName().indexOf(".")!=-1?f.getName().substring(0,f.getName().lastIndexOf("."))+"_":f.getName()+"_",f.getName());
    System.out.println("filePath===>"+filePath);
    if(f.isDirectory()){
    File[] fl=f.listFiles();
      for(File fl1:fl){
      regexReplace(fl1);
      fl1.renameTo(new File(filePath));
      }
      f.renameTo(new File(filePath));
    }else if(f.isFile()){
        f.renameTo(new File(filePath));
    }
    }
    public static String regexReplace(String filename,String name1,String name2){
    Pattern p=Pattern.compile(name2);
    String str=null;
    if(filename.indexOf(".")!=-1&&!new File(filename).isDirectory()){
    str=filename.substring(filename.lastIndexOf("."));
    }
    if(p.matcher(filename).matches()){
    filename=p.matcher(filename).replaceAll(name1);
         if(str!=null){
          filename=filename+str;      
         }
    }
    System.out.println("filename===>"+filename);
    return filename;
    }
        public static void main(String[] args){
         regexReplace(new File("D:\\Wopti-v7.83pro-xh-green"));
        }
    }
      

  8.   

    Thanks everybody and it has been successfully solved by myself.
    Note that the eight floor friend who are right.
    Thank for your help.
      

  9.   

    Thanks everybody and it has been successfully solved by myself.
    And then Note that the eight floor friend who are right.
    Thank for your help.