I have file names like 323423233.
I want to add the last 2 digits of the file name and add it to the front and make it 33/323423233 and add extension to it(like .doc).
What's a simple statement that I can use to achieve this?
I have file names like 323423233.
I want to add the last 2 digits of the file name and add it to the front and make it 33/323423233 and add extension to it(like .doc).
What's a simple statement that I can use to achieve this?
This is 2013. This is Java 7. This is the time for Files
and Path
.
Base directory:
final Path baseDir = Paths.get("/path/to/baseDir");
Determine subdirectory for a file:
final String s = name.substring(name.length() - 2, name.length());
Create that directory:
final Path subDir = baseDir.resolve(s);
// Will not do anything if directory already exists...
// But will throw exception if unable to create
Files.createDirectories(subDir);
Write to the file:
final Path dst = subDir.resolve(name + ".doc");
Files.copy(src, dst);
Remove original:
Files.delete(src);
Or in one operation:
Files.move(src, dst);