Java Rename File renameToTemporaryName(final File flFileToRename, final String strPrefix)

Here you can find the source of renameToTemporaryName(final File flFileToRename, final String strPrefix)

Description

Rename the file to temporaty name with given prefix

License

Open Source License

Parameter

Parameter Description
flFileToRename - file to rename
strPrefix - prefix to use

Exception

Parameter Description
IOException - error message

Declaration

public static void renameToTemporaryName(final File flFileToRename, final String strPrefix) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w  ww  . j a  v a 2  s.c o m*/
 *    Debrief - the Open Source Maritime Analysis Application
 *    http://debrief.info
 *
 *    (C) 2000-2014, PlanetMayo Ltd
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the Eclipse Public License v1.0
 *    (http://www.eclipse.org/legal/epl-v10.html)
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Rename the file to temporaty name with given prefix
     * 
     * @param flFileToRename - file to rename
     * @param strPrefix - prefix to use
     * @throws IOException - error message
     */
    public static void renameToTemporaryName(final File flFileToRename, final String strPrefix) throws IOException {
        assert strPrefix != null : "Prefix cannot be null.";

        String strParent;
        final StringBuffer sbBuffer = new StringBuffer();
        File flTemp;
        int iIndex = 0;

        strParent = flFileToRename.getParent();

        // Generate new name for the file in a deterministic way
        do {
            iIndex++;
            sbBuffer.delete(0, sbBuffer.length());
            if (strParent != null) {
                sbBuffer.append(strParent);
                sbBuffer.append(File.separatorChar);
            }

            sbBuffer.append(strPrefix);
            sbBuffer.append("_");
            sbBuffer.append(iIndex);
            sbBuffer.append("_");
            sbBuffer.append(flFileToRename.getName());

            flTemp = new File(sbBuffer.toString());
        } while (flTemp.exists());

        // Now we should have unique name
        if (!flFileToRename.renameTo(flTemp)) {
            throw new IOException(
                    "Cannot rename " + flFileToRename.getAbsolutePath() + " to " + flTemp.getAbsolutePath());
        }
    }
}

Related

  1. renameTo(String fileName, String targetFileName)
  2. renameToBackupName(File file)
  3. renameToBackupName(File file)
  4. renameToNextSequencedFile(String srcfile, String destfolder, String prefix, String suffix)
  5. renameToTempDir(File file, String newName)
  6. renameToUpperCase(File dir)
  7. renameWallpaper(String new_file_name, String old_file_name, Map dimmensions, String basepath, List resolution_directories)
  8. renameWithConfirm(String tmpFilename, String filename)