How to determine file type
Introduction
File type or file format is the way to classify and differentiate one kind of file from another. For example, Microsoft Excel and Adobe PDF are two different file types. The common way to determine the file type is by its extension, so when you have a file e.g. sample.docx
you expect that this file will be opened by some text processing application like Microsoft Word. But there are the cases when you don’t know the file type e.g. when the file came from the Internet but you don’t know its name or filename doesn’t have an extension.
Determining file type
GroupDocs.Viewer enables you to determine the file type by file extension (in future releases will be added possibility to determine file type by media-type and raw bytes). The code snippets provided in this article can also be found in our public examples at GitHub.
Determining file-type from the file extension
To determine file type from the file extension use fromExtension(…) method of FileType class.
String extension = ".docx";
FileType fileType = FileType.fromExtension(extension);
System.out.println("\nExtension " + extension + "; File type: " + fileType + ".");
As you can see from the output below the file type detected correctly.
D:\viewer>mvn clean compile exec:java
[INFO] Scanning for projects...
[INFO]
[INFO] ---< GroupDocs.Viewer.Examples.Java:GroupDocs.Viewer.Examples.Java >----
[INFO] Building GroupDocs.Viewer.Examples.Java
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ GroupDocs.Viewer.Examples.Java ---
Extension .docx; File type: Microsoft Word Open XML Document (.docx).
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.115 s
[INFO] Finished at: 2020-07-07T11:07:11+03:00
[INFO] ------------------------------------------------------------------------
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Viewer for Java examples, plugins, and showcase
- Document Viewer for .NET App WebForms UI Modern Example
- Document Viewer for Java App Dropwizard UI Modern Example
- Document Viewer for Java Spring UI Example
- GroupDocs.Viewer for .NET samples, plugins and showcase
- Document Viewer for .NET MVC UI Example
Free Online App
Along with full-featured Java library we provide simple but powerful free Apps. You are welcome to view your documents with free to use online GroupDocs Viewer App.