Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to get the file extension from a pathname

Reply
Thread Tools

how to get the file extension from a pathname

 
 
jimgardener
Guest
Posts: n/a
 
      07-30-2008
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.pathSeparato r);
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
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      07-30-2008
In article
<(E-Mail Removed)>,
jimgardener <(E-Mail Removed)> wrote:

> 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.pathSeparato r);
> 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 [I]
> 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/t...cal.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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
File class - conversion from abstract pathname into... sevedaja@gmail.com Java 1 06-16-2008 01:20 PM
Problem getting a file pathname with tkFileDialog cdroulers@hotmail.com Python 9 11-09-2006 01:37 PM
How to convert a .txt file extension to a .xls file extension? Steve ASP .Net 3 08-25-2006 05:43 PM
Full splitting of a file's pathname tac-tics Python 3 07-11-2006 01:04 AM



Advertisments