Java Rename File renameBackup(File backupFile)

Here you can find the source of renameBackup(File backupFile)

Description

rename a file to name.n where n is 1..2 ..3 ...

License

Apache License

Parameter

Parameter Description
backupFile existing file

Exception

Parameter Description
IllegalStateException on error

Declaration

protected static void renameBackup(File backupFile) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**/*from   w w w  .  j  a v a  2  s.c  o m*/
     * rename a file to name.n where n is 1..2 ..3 ...
     *
     * @param backupFile existing file
     * @throws IllegalStateException on error
     */
    protected static void renameBackup(File backupFile) {
        File parentDirectory = backupFile.getParentFile();
        int index = 1;
        while (true) {
            // data to a temp file
            File newName = new File(parentDirectory, backupFile.getName() + "." + index++);
            if (newName.exists()) // no worries about backup
                continue; // fina a non-existant file
            if (!backupFile.renameTo(newName))
                throw new IllegalStateException("Cannot rename  temporary file " + backupFile.getAbsolutePath()
                        + " to " + newName.getAbsolutePath());
            return;
        }

    }
}

Related

  1. rename(String srcFilename, String destFilename)
  2. rename(String srcPath, String destPath)
  3. rename(String strFileName, String strRenameToName)
  4. rename(String was_name, String new_name)
  5. rename_file_to_file(String n1, String n2)
  6. renameCanDelete()
  7. renameContigs(File aliasFile, File cytobandFile, File seqDir)
  8. renameDirectory(File directory, File newDirectory)
  9. renameDirectory(File fromDir, File toDir)