public static String replaceExtension ( String path , String newExtension ) throws IllegalArgumentException { Params . notNull ( path , "Path" ) ; Params . notNull ( newExtension , "New extension" ) ; if ( newExtension . charAt ( 0 ) == ' ' ) { newExtension = newExtension . substring ( 1 ) ; } int extensionDotIndex = path . lastIndexOf ( ' ' ) + 1 ; if ( extensionDotIndex == 0 ) { extensionDotIndex = path . length ( ) ; } StringBuilder sb = new StringBuilder ( path . length ( ) ) ; sb . append ( path . substring ( 0 , extensionDotIndex ) ) ; sb . append ( newExtension ) ; return sb . toString ( ) ; }
Replace extension on given file path and return resulting path . Is legal for new extension parameter to start with dot extension separator but is not mandatory .