Java Utililty Methods Rename File

List of utility methods to do Rename File

Description

The list of methods to do Rename File are organized into topic(s).

Method

voidrenameDirectory(String newDir, String oldDir)
rename Directory
File ofile = new File(oldDir);
File nfile = new File(newDir);
ofile.renameTo(nfile);
intrenameExcludedFiles(File existingBinaryFolder, Collection excludedBids)
Will attempt to rename each file that is bid.dat where possible bids are passed in the excludedBids collection to bid.exclude.
for (int bid : excludedBids) {
    File oldFile = new File(existingBinaryFolder, bid + "." + BINARY_FILE_EXTENSION);
    File newFile = new File(existingBinaryFolder, bid + "." + EXCLUDED_BINARY_FILE_EXTENSION);
    if (!oldFile.renameTo(newFile))
        return bid;
return -1;
voidrenameExistingWithRetries(final File fromFile, final File toFile)
rename Existing With Retries
final int MAX_TRIES = 4;
final int SLEEP_PER_TRY_MILLIS = 250;
renameExistingWithRetries(fromFile, toFile, MAX_TRIES, SLEEP_PER_TRY_MILLIS);
voidrenameFile(File src, File dest)
Rename a file.
if (dest.exists())
    dest.delete();
boolean success = false;
for (int i = 0; i < 10; i++) {
    try {
        if (i > 0)
            Thread.sleep(500);
    } catch (InterruptedException e) {
...
voidrenameFile(String source, String dest)
rename File
File file = new File(source);
file.renameTo(new File(dest));
BooleanrenameFile(String sourcePath, String targetPath)
rename File
File oldFile = new File(sourcePath);
File newFile = new File(targetPath);
if (oldFile.isFile()) {
    oldFile.renameTo(newFile);
    return true;
} else {
    return false;
booleanrenameFile(String src_pathname, String dest_pathname)
rename File
File src_file = new File(src_pathname);
return src_file.renameTo(new File(dest_pathname));
FilerenameFileFromChinese(File file)
rename File From Chinese
String fileName = file.getName();
String extension = getExtension(fileName);
String destFileName = fileName.substring(0, fileName.lastIndexOf(File.separator) + 1)
        + System.currentTimeMillis() + extension;
File destFile = new File(destFileName);
file.renameTo(destFile);
return destFile;
booleanrenameFileHard(File srcFile, File destFile)
rename File Hard
boolean isRenamed = false;
if (srcFile != null && destFile != null) {
    if (!destFile.exists()) {
        if (srcFile.isFile()) {
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
                in = new FileInputStream(srcFile);
...
FilerenameFileIfExists(File f)
* If the file f already exists, this method returns a new File object with a unique name formed by appending an integer.
File result = f;
try {
    String canonicalPath = result.getCanonicalPath();
    int lidx = canonicalPath.lastIndexOf(".");
    String suff = "";
    String base = canonicalPath;
    if (lidx != -1) {
        suff = canonicalPath.substring(lidx);
...