Ever wonder why this life is so messed up? I sure as hell do! I don’t get it. I mean we’re here for a reason…maybe. I don’t understand anything about it though. I wish I did, it would be nice to be able to grasp something about this life and not simply expect it to be here whenever we need it to be. Yet that’s exactly what we do! We expect life to be here for us and we don’t want to give anything back, talk about an annoying thought process. But what are we going to do about it all? I don’t know. I wish I did, but there are so many things in this life that don’t quite make sense at times, and we are here trying to make sense of them all. It sucks, it really does. But what else are you supposed to do about it? I don’t know.
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