Here you can find the source of renameToTemporaryName(final File flFileToRename, final String strPrefix)
Parameter | Description |
---|---|
flFileToRename | - file to rename |
strPrefix | - prefix to use |
Parameter | Description |
---|---|
IOException | - error message |
public static void renameToTemporaryName(final File flFileToRename, final String strPrefix) throws IOException
//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()); } } }