Here you can find the source of renameBackup(File backupFile)
Parameter | Description |
---|---|
backupFile | existing file |
Parameter | Description |
---|---|
IllegalStateException | on error |
protected static void renameBackup(File backupFile)
//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; } } }