File Extension Groups in JAVA

File Extension Groupings – Why?

File extensions groups allow you to group files. So due to the file extensions you can report that this group of files are images (graphics), these are programming source files

  • Java – .java
  • PHP – .php
  • Perl – .pl
  • JavaScript – .js

Note that a file extension can be classed as (and appear in) more than one file group. For example

  • .xls is an Office file (grouped with docx and ppt) – but also a spreadsheet (as is say Lotus Notes)
  • css is both a native browser file (grouped with html, js, png) – but also a stylesheet (with Akka)

File Extension Groupings – JAVA

The file extensions are encapsulated by this JAVA enum accumulator construct. For the sake of brevity below is just
a small fraction of all file extensions groupings. That way you can work with File extensions in a fuss-free way.

(Note also that the same file extension functionality is available as a Web Services (Restful) and other common B2B protocols.

File Extension Grouping – JAVA ENUM CLASS


package machine; 
 
import java.util.Arrays; 
import java.util.HashSet; 
import java.util.Set; 
 
/** 
 * Useful file extension groupings as JAVA enums that you can just plug into 
 * your code. These file extensions are also available as web services if you 
 * need to ensure continuity when new file extensions come on line along with 
 * new popular technologies. 
 * 
 * If you need file extensions that are currently popular and future file extensions 
 * are not of great concern then just copy and paste in this file. 
 * 
 * If your file extension needs are dynamic and more extensive (ie the full list on 
 * our database runs into the tens of thousands of file extensions) then contact 
 * us for the web services implementations. 
 * 
 * File extensions access protocols supported are Restful HTTP format, SOAP, COM 
 * C.O.R.B.A and bog-standard XML.The file extensions are also available in relational 
 * database formats (Oracle, MySQL and SQLServer), CSV (comma separated value) files, 
 * Tab delimited files, XML files, PDF files and Microsoft Excel (xls) spreadsheets. 
 * 
 * These file extensions work well with the Apache Commons File API. 
 * 
 * File extension case fluctuates so be prepared to apply JAVA's String toLowerCase() 
 * or compare file extensions with String equalsIgnoreCase method. 
 */ 
public enum FileExtensions { 
 
    /** 
     * Image (graphics) file extensions 
     */ 
    IMAGE_FILE_EXTENSIONS ( "jpeg", "jpg", "png", "gif" ), 
 
    /** 
     * Music (sound) file extensions 
     */ 
    MUSIC_FILE_EXTENSIONS ( "mp3", "wav" ), 
 
    /** 
     * Moving image, videos, movies file extensions 
     */ 
    MOVING_IMAGE_FILE_EXTENSIONS ( "mp4", "avi" ), 
 
    /** 
     * INI configuration file extensions. 
     */ 
    INI_CONFIGURATION_FILE_EXTENSIONS ( "ini", "INI" ), 
 
    /** 
     * PHP file extensions 
     */ 
    PHP_FILE_EXTENSIONS ( "php" ), 
 
    /** 
     * All file extensions base option (for completeness). 
     */ 
    ALL_EXTENSIONS; 
 
    public static final String[] PHP_SCRIPTS = { "php" }; 
    public static final String[] CSS_STYLESHEETS = { "css" }; 
 
    private String[] fileExtensions; 
 
    /** 
     * Get the list of file extensions as a String array. 
     * @return the file extensions as a String array. 
     */ 
    public String[] getFileExtensionsAsStringArray(){ 
        return this.fileExtensions; 
    } 
 
    /** 
     * Get the list of file extensions as a set of strings. 
     * @return the file extensions as a set of strings 
     */ 
    public Set<String> getFileExtensionsAsSet(){ 
        Set<String> extensionsSet = new HashSet<String>(); 
        for( String fileExtension: fileExtensions ) 
            extensionsSet.add( fileExtension ); 
 
        return extensionsSet; 
    } 
 
    /** 
     * Get the list of file extensions as just one long 
     * line of String values. 
     * @return the file extensions as one long string. 
     */ 
    public String getFileExtensionsAsOneString(){ 
        return Arrays.toString( this.fileExtensions ); 
    } 
 
    /** 
     * Construct the file extensions enum object privately. 
     * This constructor represents the NO file extensions case. 
     */ 
    private FileExtensions(){ 
    } 
 
    private FileExtensions( String fileExt1 ){ 
        String[] theseFileExtensions = new String[1]; 
        theseFileExtensions[0] = fileExt1; 
        this.fileExtensions = theseFileExtensions; 
    } 
 
    private FileExtensions( String fileExt1, String fileExt2 ){ 
        String[] theseFileExtensions = new String[2]; 
        theseFileExtensions[0] = fileExt1; 
        theseFileExtensions[1] = fileExt2; 
        this.fileExtensions = theseFileExtensions; 
    } 
 
    private FileExtensions( String fileExt1, String fileExt2, String fileExt3 ){ 
        String[] theseFileExtensions = new String[3]; 
        theseFileExtensions[0] = fileExt1; 
        theseFileExtensions[1] = fileExt2; 
        theseFileExtensions[2] = fileExt3; 
        this.fileExtensions = theseFileExtensions; 
    } 
 
    private FileExtensions( String fileExt1, String fileExt2, String fileExt3, String fileExt4 ){ 
        String[] theseFileExtensions = new String[4]; 
        theseFileExtensions[0] = fileExt1; 
        theseFileExtensions[1] = fileExt2; 
        theseFileExtensions[2] = fileExt3; 
        theseFileExtensions[3] = fileExt4; 
        this.fileExtensions = theseFileExtensions; 
    } 
 
} 

Leave a Reply

Your email address will not be published. Required fields are marked *