Friday, May 13, 2011

Get File Name without extension from Its Path

The following Java method returns a file name without extension from its path.

public static String getFileName(String path){

 String fileName = null;
 String separator = File.separator;

 int pos = path.lastIndexOf(separator);
 int pos2 = path.lastIndexOf(".");

 if(pos2>-1)
  fileName =path.substring(pos+1, pos2);
 else
  fileName =path.substring(pos+1);

 return fileName;
}
 

No comments:

Post a Comment