Antonis
Antonis

Reputation: 1061

Rename a file to combine the names of two files

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

Answers (2)

Bozho
Bozho

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

Michał Niklas
Michał Niklas

Reputation: 54312

To rename file use renameTo() method of File class. Use methods of String class to manipulate their names.

Upvotes: 0

Related Questions