Here you can find the source of renameFiles(String rootDir, String sourcePostfix, String destPostfix)
true
when rename successfully,otherwise return false
Parameter | Description |
---|---|
file | The root file |
sourcePostfix | The scource file extension |
destPostfix | The destination file extension |
public static File[] renameFiles(String rootDir, String sourcePostfix, String destPostfix)
//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); } }