how to get the file extension from a pathname

Discussion in 'Java' started by jimgardener, Jul 30, 2008.

  1. jimgardener

    jimgardener Guest

    hi
    i want to extract the extension of a file as a String.Suppose the
    pathname of file is F:\mycode\myimage\newimg.jpg
    i need to get the extension 'jpg' only
    I did it like this,

    public static void main(String [] args){
    String imagetomatch=args[0];
    String[] words=imagetomatch.split(java.io.File.pathSeparator);
    String ext=(words[words.length-1].split("\\.")[1]);
    System.out.println("ext="+ext);
    }

    is there a better/compact way to do this?

    Also ,why does'nt the .split(".") work in the above case.Why do i have
    to use .split("\\.")?
    thanks
    jim
    jimgardener, Jul 30, 2008
    #1
    1. Advertising

  2. In article
    <>,
    jimgardener <> wrote:

    > hi
    > want to extract the extension of a file as a String.Suppose the
    > pathname of file is F:\mycode\myimage\newimg.jpg need to get the
    > extension 'jpg' only. I did it like this,
    >
    > public static void main(String [] args){
    > String imagetomatch=args[0];
    > String[] words=imagetomatch.split(java.io.File.pathSeparator);
    > String ext=(words[words.length-1].split("\\.")[1]);
    > System.out.println("ext="+ext);
    > }
    >
    > is there a better/compact way to do this?


    You might look at java.io.File#getName()

    <http://java.sun.com/javase/6/docs/api/java/io/File.html#getName()>

    > Also, why does'nt the .split(".") work in the above case.Why do
    > have to use .split("\\.")?


    The symbol '.' represents one of the predefined character classes (any
    character) in a java.util.regex.Pattern:

    <http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html>

    In contrast, you're looking for the literal character '\u002E'
    (full-stop, period, dot, decimal, etc.) A regex parameter containing a
    single backslash ("\.") would be seen as an illegal escape character:

    <http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#25687
    >


    Using two backslashes signals that the second one is a literal '\'
    character to be sent to the regex parser, which then uses it to escape
    the special meaning of '.'.

    [It's like a day at the beach, littorally.]

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
    John B. Matthews, Jul 30, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Vercuski

    Access to the path <pathname> is denied.

    Scott Vercuski, Feb 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    523
    Arvind P Rangan
    Feb 25, 2004
  2. tac-tics
    Replies:
    3
    Views:
    303
    Simon Forman
    Jul 11, 2006
  3. Steve
    Replies:
    3
    Views:
    26,975
    Steve
    Aug 25, 2006
  4. Replies:
    9
    Views:
    989
    Neil Cerutti
    Nov 9, 2006
  5. Replies:
    1
    Views:
    609
    voorth
    Jun 16, 2008
Loading...

Share This Page