I want to get the file extension in my Java code:
File f = new File(....);
String name = f.getName();
int pos = name.lastIndexOf('.');
String ext = name.substring(pos+1);
I tried this code but it didn’t work in OS X Lion, in Windows it works.
UPDATE
for example
i have a png file in my desktop with this name Screen Shot 2012-02-23 at 1.26.27 PM
i get the 27 PM extension with this code .
Your code is doing exactly what you’ve asked it to do. It’s finding the filename extension on the file. The problem is that you have file with a stupid name that should have a “.png” extension, but doesn’t. If you still want your code to handle this, you might need to look at interpreting the file header to determine the type of the file, which is presumably what OS X is doing to know that it is really a .png file.