patternjavaMinor
Adding "_old" to a file name
Viewed 0 times
_oldfilenameadding
Problem
This is a trivial problem. The reason I'm here is to get a better idea of how to use Java's numerous APIs for working with files, folders, and paths. I'm hoping this specific example will help.
Problem Statement
I want to rename a file like this:
/path/to/myFile.txt
to this:
/path/to/myFile_old.txt
Ugly Solution
I've been trying to find a concise way to do this, and this is the best I can come up with:
It works, which is why I'm not on Stack Overflow, but
I've looked at using
Problem Statement
I want to rename a file like this:
/path/to/myFile.txt
to this:
/path/to/myFile_old.txt
Ugly Solution
I've been trying to find a concise way to do this, and this is the best I can come up with:
Files.move(
Paths.get(file),
Paths.get("/" + FilenameUtils.getPath(file) + FilenameUtils.getBaseName(file) + "_old" + FilenameUtils.getExtension(file)));It works, which is why I'm not on Stack Overflow, but
- It's not portable because of the hardcoded "/".
- It's really cumbersome -- specifically I don't like how verbose
org.apache.commons.io.FilenameUtilsis, and how I have to convert myStrings toPaths withPaths.get.
I've looked at using
java.io.File or java.nio.file.Paths.resolve... but I haven't found anything tidy.Solution
I think that the approach of the question lacks only a better usage of the API of
Using the old
Other ways to build the backup file name: use
java.nio.file.Path.Using the old
java.io.File seems very ugly nowadays, when we have java.nio.*, where all the stuff like file separators is supported much better.// ...
String originalFile = "/path/to/myFile.txt";
Path originalPath = Paths.get(originalFile);
String backupFileName = buildBackupFileName(originalPath.getFileName().toString());
Path backupPath = originalPath.getParent().resolve(backupFileName);
Files.move(originalPath, backupPath); // add options if necessary
// ...
private static String buildBackupFileName(String fileName) {
String backupSuffix = "_old"; // or move it in a static constant
int lastDotIndex = fileName.lastIndexOf(".");
if (lastDotIndex > -1) {
return fileName.substring(0, lastDotIndex) + backupSuffix + fileName.substring(lastDotIndex);
}
return fileName + backupSuffix;
}Other ways to build the backup file name: use
StringBuilder or String.format, with or without the above FilenameUtils.Code Snippets
// ...
String originalFile = "/path/to/myFile.txt";
Path originalPath = Paths.get(originalFile);
String backupFileName = buildBackupFileName(originalPath.getFileName().toString());
Path backupPath = originalPath.getParent().resolve(backupFileName);
Files.move(originalPath, backupPath); // add options if necessary
// ...
private static String buildBackupFileName(String fileName) {
String backupSuffix = "_old"; // or move it in a static constant
int lastDotIndex = fileName.lastIndexOf(".");
if (lastDotIndex > -1) {
return fileName.substring(0, lastDotIndex) + backupSuffix + fileName.substring(lastDotIndex);
}
return fileName + backupSuffix;
}Context
StackExchange Code Review Q#110720, answer score: 4
Revisions (0)
No revisions yet.