For example, if I have a video file and I change the mp4 extension to txt, its internal structure is still that of a video. But how could it detect that it is still a video if the extension has been changed. I have done some research and I know that each extension has certain bytes or a certain structure that identifies them but I don't know how to use that to verify what my question says.
You could determine the MIME TYPE of the file, using Files.probeContentType() .
For example for a file called file_video.mp4, the MIME TYPE for this type of file is:
video/mp4
, if you change the extension; via Files.probeContentType() you could get the type and determine what type of content your file really is:You can open the file as RandomAccessFile and read the file's metadata (depending on the specifications of the file you expect to find).
In the case of ISO containers, for example, you have to find the ftyp box , which has the following format:
the rest of the data is already optional, more information (in English) you find here , a list of major types here .
With an ISO-based video file, you can get the largest type like this:
(This code should not be used in a productive system, it should be ensured that system resources are released in case of errors.)