java对文件重命名

来源:互联网 发布:手机淘宝卖家页面 编辑:程序博客网 时间:2024/05/16 10:48

将目录下文件名中有"_*"的文件名重命名为去掉“_*”后的名字
public void renameFiles(String path)
    {
        File file = new File(path);
        File[] files = file.listFiles();
        for(File f : files)
        {
            if(f.isDirectory())
            {
                File ff = new File(f.getAbsolutePath());
                for(File f1 : ff.listFiles())
                {
                    String tem = f1.getName();
                    if(tem.contains("_"))
                    {
                        tem = tem.substring(0, tem.indexOf("_")) + tem.substring(tem.lastIndexOf("."));
                        File newFile = new File("" + ff.getName() + "/" + tem);
                        f1.renameTo(newFile);
                    }
                }
            }
        }
    }  
0 0