Java Rename File renameFiles(String rootDir, String sourcePostfix, String destPostfix)

Here you can find the source of renameFiles(String rootDir, String sourcePostfix, String destPostfix)

Description

Recursive rename the child file with given extension to another extension of root directory.Return true when rename successfully,otherwise return false

License

Apache License

Parameter

Parameter Description
file The root file
sourcePostfix The scource file extension
destPostfix The destination file extension

Return

The file object that failde rename

Declaration

public static File[] renameFiles(String rootDir, String sourcePostfix, String destPostfix) 

Method Source Code

//package com.java2s;
/**//from  w  w w. j  a v a2s.  c o  m
 * Copyright 2004-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * History: 2007-2-4 13:24:10 Created by guyang
 */

import java.io.File;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Recursive rename the child file with given extension to another extension
     * of root directory.Return <code>true</code> when rename
     * successfully,otherwise return <code>false</code>
     * 
     * @param file The root file
     * @param sourcePostfix The scource file extension
     * @param destPostfix The destination file extension
     * @return The file object that failde rename
     */
    public static File[] renameFiles(String rootDir, String sourcePostfix, String destPostfix) {
        File[] renamedFailedFiles = new File[0];
        List<File> fileList = new ArrayList<File>();
        File rootFile = new File(rootDir);
        File[] subFiles = rootFile.listFiles();
        for (File file : subFiles) {

            if (file.isDirectory()) {
                File[] tempRenamedFailedFiles = renameFiles(file.getPath(), sourcePostfix, destPostfix);
                for (File tempFile : tempRenamedFailedFiles) {
                    fileList.add(tempFile);
                }
                continue;
            }

            String tempPath = file.getPath();
            String distFilePath = tempPath.replaceAll(sourcePostfix, destPostfix);
            File distFile = new File(distFilePath);
            if (!file.renameTo(distFile)) {
                fileList.add(file);
            }
        }
        return fileList.toArray(renamedFailedFiles);
    }
}

Related

  1. renameFileFromChinese(File file)
  2. renameFileHard(File srcFile, File destFile)
  3. renameFileIfExists(File f)
  4. renameFileOnWindows(File oldFile, File newFile)
  5. renameFiles(File fileDirectory, String regex, String replacement)
  6. renameFolder(String destinationFolderPath, String zipFileName, String prefix)
  7. renameLocal(String oldPath, String newPath)
  8. renameOldFile(final String fileName)
  9. renameOverwrite(String oldname, String newname)