Java Rename File renameToBackupName(File file)

Here you can find the source of renameToBackupName(File file)

Description

rename To Backup Name

License

Open Source License

Declaration

public static void renameToBackupName(File file) throws IOException 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.io.File;

import java.io.IOException;

public class Main {
    public static void renameToBackupName(File file) throws IOException {
        File backupFile = new File(file.getParentFile(), file.getName() + "~");
        if (backupFile.exists()) {
            boolean deleted = backupFile.delete();
            if (!deleted) {
                throw new IOException("Cannot delete backup file " + backupFile);
            }// w w w  . j  av  a  2 s .  com
        }
        boolean renamed = file.renameTo(backupFile);
        if (!renamed) {
            throw new IOException("Cannot create backup file for " + file);
        }
    }

    public static void delete(File file, boolean recursive) throws IOException {
        if (file.exists()) {
            if (file.isDirectory() && recursive) {
                for (File childFile : file.listFiles()) {
                    delete(childFile, recursive);
                }
            }
            if (!file.delete()) {
                throw new IOException("Cannot delete file " + file);
            }
        }
    }
}

Related

  1. renameTo(File orig, File dest)
  2. renameTo(File source, String newName)
  3. renameTo(final File srcFile, final File dstfile)
  4. renameTo(String fileName, String targetFileName)
  5. renameToBackupName(File file)
  6. renameToNextSequencedFile(String srcfile, String destfolder, String prefix, String suffix)
  7. renameToTempDir(File file, String newName)
  8. renameToTemporaryName(final File flFileToRename, final String strPrefix)
  9. renameToUpperCase(File dir)