Reputation: 1061
I have the following files in my filesystem: file1_mp4
and file2_3gp
.
I want to rename the second file to the name of the left half of the first file file1
and the extension _3gp
from the second file, producing file1_3gp
.
Upvotes: 3
Views: 375
Reputation: 597124
Since the underscore is not actually an extension separator, you'd have to split the name:
String[] parts1 = file1.getName().split("_");
String[] parts2 = file2.getName().split("_");
Then you can rename
file1.renameTo(parts1[0] + "_" + parts2[1]);
(above, file1
and file2
are instances of java.io.File
)
Upvotes: 3
Reputation: 54312
To rename file use renameTo() method of File
class. Use methods of String
class to manipulate their names.
Upvotes: 0