And there exists no reliable function for it. Consider for example this filename:
archive.tar.gz
What is the extension? DOS users would have preferred the name archive.tgz
. Sometimes you see stupid Windows applications that first decompress the file (yielding a .tar
file), then you have to open it again to see the archive contents.
In this case, a more reasonable notion of file extension would have been .tar.gz
. There are also .tar.bz2
, .tar.xz
, .tar.lz
and .tar.lzma
file "extensions" in use. But how would you decide, whether to split at the last dot, or the second-to-last dot?
The Java 7 function Files.probeContentType will likely be much more reliable to detect file types than trusting the file extension. Pretty much all the Unix/Linux world as well as your Webbrowser and Smartphone already does it this way.