Tue Jan 7 19:39:37 MST 2020 Let’s talk about today than shall we? I’d like to think it would be a nice moment in time if we could simply get along with everything that happens in this life, yet I doubt it will. It’s a shame if you think about it. A real shame. But what are you going to do with any of it? No one knows exactly. So here we sit waiting for something better to come along, hoping for something to happen and allowing us to actually see what is real and what isn’t.
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