One would think this life would be easier than it currently is. I mean come on wouldn’t ou think that? Of course you would. So why can’t we each believe in it I for one would want to believe in it. It would give me something to do with my day at least. But well there’s something else on my mind. We have to be able to understand what it is exactly, if we don’t? Then that’s something quite different now isn’t it? Yeah that’s what I was thinking.
So this little method will return the file extension of a file. That is, it will look for the dot (.) in a filename and return the remaining characters after it.
Notice that it makes sure it's a file and not a directory.
/**
* Returns the extension of a file
*
* @param filename the name of the file
* @return the extension of the filename
*/
public static String getExtension(String filename) {
String ext = null;
File file = new File(filename);
if (file.exists() && file.isFile()) {
ext = filename.substring(filename.lastIndexOf(".") + 1);
}
return ext;
}
Pretty simple no? Yeah that's what I was thinking.
You can easily put this in a FileUtils class that you might be building. It could come in handy.
Please remember that the file extension does not dictate what the file type is. It's just an extension.
Comments
Post a Comment