Java Rename File renameToNextSequencedFile(String srcfile, String destfolder, String prefix, String suffix)

Here you can find the source of renameToNextSequencedFile(String srcfile, String destfolder, String prefix, String suffix)

Description

rename To Next Sequenced File

License

Open Source License

Declaration

public static File renameToNextSequencedFile(String srcfile, String destfolder, String prefix, String suffix)
            throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    private static String renameToNextSequencedFileLock = "renameToNextSequencedFile";

    public static File renameToNextSequencedFile(String srcfile) throws Exception {
        return renameToNextSequencedFile(srcfile, null, null, null);
    }/* w  ww  .  ja va  2 s  .c  om*/

    public static File renameToNextSequencedFile(String srcfile, String destfolder, String prefix, String suffix)
            throws Exception {
        synchronized (renameToNextSequencedFileLock) {
            File src = new File(srcfile);
            File dest = null;
            int count = 0;
            if (destfolder == null) {
                if (src.getParent() != null)
                    destfolder = src.getAbsoluteFile().getParent();
                else
                    destfolder = "";
            }
            int pos;
            if (prefix == null) {
                if ((pos = srcfile.lastIndexOf('.')) != -1) {
                    prefix = srcfile.substring(0, pos);
                    if (suffix == null)
                        suffix = srcfile.substring(pos);
                }
            }
            while (true) {
                dest = new File(destfolder + prefix + "_" + count++ + suffix);
                if (!dest.exists()) {
                    break;
                }
            }
            if (!src.renameTo(dest))
                throw new Exception("unable to rename " + src + " to " + dest);
            if (src.exists() && !src.delete())
                src.deleteOnExit();
            return dest;
        }
    }
}

Related

  1. renameTo(File source, String newName)
  2. renameTo(final File srcFile, final File dstfile)
  3. renameTo(String fileName, String targetFileName)
  4. renameToBackupName(File file)
  5. renameToBackupName(File file)
  6. renameToTempDir(File file, String newName)
  7. renameToTemporaryName(final File flFileToRename, final String strPrefix)
  8. renameToUpperCase(File dir)
  9. renameWallpaper(String new_file_name, String old_file_name, Map dimmensions, String basepath, List resolution_directories)