string - Cleaning a file name in Java -
i want write script clean .mp3 files. able write few line change name want write automatic script erase undesired characters $%_!?7 , etc. while changing name in next format artist space dash song.
file file = new file("c://users//nikita//desktop//$%#artis8t_-_35&son5g.mp3"); string original = file.tostring(); string new = "code change 'original' 'artist - song'"; file file2 = new file("c://users//nikita//desktop//" + new + ".mp3"); file.renameto(file2); i feel should make list possible characters , run string through list , erase of listed characters not sure how it.
string test = "$%$#arti56st_-_54^so65ng.mp3"; edit 1:
when try using method remove, still doesn't change name.
string test = "$%$#arti56st_-_54^so65ng.mp3"; system.out.println("original: " + test); test.replace( "[0-9]%#&\\$", ""); system.out.println("new: " + test); the code above returns following output
original: $%$#arti56st_-_54^so65ng.mp3 new: $%$#arti56st_-_54^so65ng.mp3
i'd suggest this:
public static string santizefilename(string original){ pattern p = pattern.compile("(.*)-(.*)\\.mp3"); matcher m = p.matcher(original); if (m.matches()){ string artist = m.group(1).replaceall("[^a-za-z ]", ""); string song = m.group(2).replaceall("[^a-za-z ]", ""); return string.format("%s - %s", artist, song); } else { throw new illegalargumentexception("failed match filename : "+original); } } (edit - changed whitelist regex exclude digits , underscores)
two points in particular - when sanitizing strings, it's idea whitelist permitted characters, rather blacklisting ones want exclude, won't surprised edge cases later. (you may want less restrictive whitelist i've used here, it's easy vary) it's idea handle case filename doesn't match expected pattern. if code comes across other mp3, how respond? here i've through exception, calling code can catch , handle appropriately.
Comments
Post a Comment