how-to

File & Directory

How to get file extention

When you need to separate file extention, use this function:

String GetFileExtension( String fileName ) {

  int iPos = fileName.lastIndexOf( "." );   // get last occurrence of "." char

  if ( iPos == -1 )
    return( "" );
  else
    return( fileName.substring( iPos ) );
}
..
..
..
System.out.print( GetFileExtension( "c:\\readme.txt" ) );
This example writes ".txt" string.