Java Utililty Methods File Rename

List of utility methods to do File Rename

Description

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

Method

voidrenameFile(File file, String ext)
rename File
if (!file.exists()) {
    return;
File dest = new File(file.getParent(), file.getName() + ext);
boolean success = file.renameTo(dest);
if (success) {
    System.out.println("[INFO] Rename Session LOG " + dest);
} else {
...
FilerenameFile(File file, String name)
rename File
String filePath = file.getAbsolutePath();
String fileName = file.getName();
String newFilePath = filePath.substring(0, filePath.length() - fileName.length()) + name;
File newFile = new File(newFilePath);
file.renameTo(newFile);
return newFile;
booleanrenameFile(File file, String newFilename)
rename File
if (file.exists() == false)
    return false;
String path = file.getPath();
File new_file = new File(path + File.separator + newFilename);
return file.renameTo(new_file);
voidrenameFile(File file, String newName)
rename File
file.renameTo(new File(newName));
FilerenameFile(File file, String newName)
TODO
if (file.exists()) {
    File dest = new File(file.getParentFile(), newName);
    if (file.renameTo(dest)) {
        return dest;
return null;
FilerenameFile(File file, String newName)
rename File
String properPath = file.getPath().substring(0, file.getPath().lastIndexOf(File.separator));
String newNameWithoutExt = newName.substring(0,
        newName.lastIndexOf(".") == -1 ? newName.length() : newName.lastIndexOf("."));
return new File((properPath + File.separator + newNameWithoutExt + ".srt").replaceAll("\\s", "_"));
voidrenameFile(File file, String newName)
rename File
if (file == null)
    throw new NullPointerException("file must not be null");
if (!file.isFile() || !file.exists())
    throw new IllegalArgumentException("file must not be a file and exists");
File newFile = new File(file.getParent() + "/" + newName);
file.renameTo(newFile);
voidrenameFile(File file, String sign, String suffix)
rename File
String name = file.getAbsolutePath();
int index = name.lastIndexOf(suffix);
String newName = name.substring(0, index) + "_" + sign + name.substring(index, name.length());
file.renameTo(new File(newName));
voidrenameFile(File source, File dest)
Traduction to file
source.renameTo(dest);
voidrenameFile(File sourceFile, String newFileName)
rename File
File targetFile = new File(sourceFile.getParent(), newFileName);
sourceFile.renameTo(targetFile);