How Do I Check for Extensions?

Posted on 2004-11-05
Last Modified: 2010-04-17

How would I do a check for extensions?

For example, if I'd like to check the database for an image file name, and if the image file name has extension ".jpg" or ".jpeg" or ".gif", to not append the ".gif" extension.

However, if the ".someExtension" does not exist, I want to append the ".gif" extension.

In pseudocode something like this:

public String getBannerImage(){
  String bImage="";
  String bName=(String)storedValueForKey("bannerImageFile");
  if bName ends with .someExtension
       do Nothing;
    bName= add Extension( (bName)+".gif");

Any assistance or guidance would be greatly appreciated!

Question by:jmc430
    LVL 48

    Expert Comment

    you would do an right(filename,Instr(filename,".") ) and that gets the ext

    Author Comment

    is that in java?

    Author Comment

    eh .. nevermind
    LVL 9

    Expert Comment


    So, how would you like this method/function to be used? How is the filename passed, and if there exists an extension, does the user pass the filename to the method/function?

    Also, what language do you want it in? ...... Java?

    LVL 9

    Accepted Solution

    Here's something in Java, that should work:

        public String getBannerImage( ) {
            String bName=(String)storedValueForKey( "bannerImageFile" );
            StringTokenizer st = new StringTokenizer( bName, "." );
            int temp = st.countTokens();
            String sTemp = "";
            for ( int i=0; i<temp; i++ )
                sTemp = st.nextToken();
            if ( sTemp==null ) return;
            String [] ext = { "jpg", "gif", "jpeg" };
            boolean bTemp = false;
            for ( int i=0; i<ext.length; i++ ) {
                if ( sTemp == ext[i] ) bTemp = true;
            if ( !bTemp ) {
                // Rename file, and add extension:
                File file = new File( bName );
                File file2 = new File( bName + ".gif" );
                boolean success = file.renameTo(file2);
                if ( !success ) {
                    System.out.println( "File Could Not Be Renamed." );

    Note: you need to import the:*   and   java.util.*  packages, for the above method to work.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now