28/07/2013

[Java] Get extension from filename

To extract the extension from a given filename in Java, you may try:

 String yourfilename = "something.ext";  
 String[] tokens = yourfilename.split("\\.(?=[^\\.]+$)");  
 String extension = tokens[1].toLowerCase();  

This works even if filename contains multiple dots, however it's not an accurate way of determining a file type.

No comments:

Post a Comment

With great power comes great responsibility