Java Rename File renameToTempDir(File file, String newName)

Here you can find the source of renameToTempDir(File file, String newName)

Description

rename To Temp Dir

License

LGPL

Declaration

public static File renameToTempDir(File file, String newName) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.File;

public class Main {
    public static File renameToTempDir(File file, String newName) {
        return renameToTempDir(file, newName, null, null, 1);
    }/*w w  w  . j a v a 2s  . c  om*/

    public static File renameToTempDir(File file, String newName, String prefix) {
        return renameToTempDir(file, newName, prefix, null, 1);
    }

    public static File renameToTempDir(File file, String newName, String prefix, String suffix, int attempts) {
        File result = null;
        if (file != null && file.exists()) {
            File folder = createTempFolder(prefix, suffix, attempts);
            result = new File(folder, newName);
            file.renameTo(result);
        }
        return result;
    }

    public static File createTempFolder() {
        return createTempFolder(null, null, 1);
    }

    public static File createTempFolder(String prefix) {
        return createTempFolder(prefix, null, 1);
    }

    public static File createTempFolder(String prefix, String suffix) {
        return createTempFolder(prefix, suffix, 1);
    }

    public static File createTempFolder(String prefix, String suffix, int attempts) {
        File result = null;

        prefix = (prefix != null ? prefix : "");
        suffix = (suffix != null ? suffix : "");

        String tmpDir = System.getProperty("java.io.tmpdir");

        File baseDir = new File(tmpDir);
        String baseName = "" + System.currentTimeMillis();

        for (int counter = 0; counter < attempts; counter++) {
            String counterStr = (counter > 0 ? "-" + counter : "");
            String fileName = prefix + baseName + suffix + counterStr;
            File tempDir = new File(baseDir, fileName);
            if (tempDir.mkdir()) {
                result = tempDir;
                break;
            }
        }
        if (result == null) {
            throw new IllegalStateException("Failed to create directory");
        }

        return result;
    }
}

Related

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