Java Utililty Methods Rename File

List of utility methods to do Rename File

Description

The list of methods to do Rename File are organized into topic(s).

Method

booleanrenameFileOnWindows(File oldFile, File newFile)
rename File On Windows
String sCurrentLine = "";
try {
    BufferedReader bufReader = new BufferedReader(new FileReader(oldFile.getAbsoluteFile()));
    BufferedWriter bufWriter = new BufferedWriter(new FileWriter(newFile.getAbsoluteFile()));
    while ((sCurrentLine = bufReader.readLine()) != null) {
        bufWriter.write(sCurrentLine);
        bufWriter.newLine();
    bufReader.close();
    bufWriter.close();
    oldFile.delete();
    return true;
} catch (Exception e) {
    return false;
voidrenameFiles(File fileDirectory, String regex, String replacement)
rename Files
for (File file : fileDirectory.listFiles()) {
    File newFile = new File(file.getAbsolutePath().replaceFirst(regex, replacement));
    file.renameTo(newFile);
File[]renameFiles(String rootDir, String sourcePostfix, String destPostfix)
Recursive rename the child file with given extension to another extension of root directory.Return true when rename successfully,otherwise return false
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) {
...
voidrenameFolder(String destinationFolderPath, String zipFileName, String prefix)
rename Folder
File folder = new File(destinationFolderPath + File.separator + zipFileName);
File managerFolder = new File(destinationFolderPath + File.separator + prefix + zipFileName);
if (folder.exists()) {
    folder.renameTo(managerFolder);
} else {
    System.out.println(zipFileName + " Folder not found..!!");
booleanrenameLocal(String oldPath, String newPath)
rename Local
System.out.println("Renaming local item " + oldPath + " to " + newPath);
File oldFile = new File(oldPath);
File newFile = new File(newPath);
return (oldFile.renameTo(newFile));
voidrenameOldFile(final String fileName)
rename Old File
final File file = new File(fileName);
if (file.exists()) {
    final File oldFile = new File(fileName + ".old");
    if (oldFile.exists()) {
        oldFile.delete();
    file.renameTo(oldFile);
voidrenameOverwrite(String oldname, String newname)
Rename the file with oldname to newname.
try {
    if (exists(oldname)) {
        delete(newname);
        File file = new File(oldname);
        file.renameTo(new File(newname));
} catch (Throwable e) {
    throw toIOException(e);
...
FilerenameSuffix(File f, String suffix)
rename Suffix
if (null == f || null == suffix || suffix.length() == 0)
    return f;
return new File(renameSuffix(f.getAbsolutePath(), suffix));
voidrenameTempDMLScript(String dmlScriptFile)

Renames a temporary DML script file back to it's original name.

File oldPath = new File(dmlScriptFile + "t");
File newPath = new File(dmlScriptFile);
oldPath.renameTo(newPath);
voidrenameTo(File currentFile, String newName)
Platform independent rename function
try {
    File newFile = new File(newName);
    Files.move(currentFile, newFile);
} catch (IOException e) {
    e.printStackTrace();