Results 1 to 2 of 2
  1. #1
    Rubin is offline Member
    Join Date
    Aug 2007
    Posts
    1
    Rep Power
    0

    Default How to compare file extensions?

    Hi,
    i wanna compare the extension of a file with a particular string... can u please help me to do this.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
        public static void main(String[] args) {
            File file = new File("images/cougar.jpg");
            String ext = getExtension(file);
            String JPG = "jpg";
            if(JPG.equals(ext))
                System.out.println("equal");
            else
                System.out.println("not equal");
        }
    
        private static String getExtension(File file) {
            String s = file.getPath();
            int dot = s.lastIndexOf(".");
            if(dot > -1 && dot < s.length())
                return s.substring(dot+1);
            return null;
        }

Similar Threads

  1. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 03:22 PM
  2. Deleting files after filtering the extensions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-25-2008, 08:00 PM
  3. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 03:58 AM
  4. myfaces.apache.org/extensions can't resolve
    By Heather in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 06-30-2007, 05:46 PM
  5. compare speed
    By bbq in forum JDBC
    Replies: 1
    Last Post: 06-28-2007, 06:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •