Re: how to get the file extension from a pathname



In article
<2796007b-cc6d-42c0-894f-3d73330cd73c@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
jimgardener <jimgardener@xxxxxxxxx> 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.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 [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/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
.



Relevant Pages

  • Re: Make all files extension lower by a given directory name
    ... are you sure you want to strip off the last character in the actual ... you'd want to strip the dot off the left side of the extension rather than try and monkey with the right side of the filename portion. ...
    (comp.lang.python)
  • Re: If exist batch command not working in WinXp
    ... vanilla DOS through at least 5.0, unless I am very much mistaken. ... "The * Character" ... with AB and have an extension of XYZ. ... or *.* matches anything with or without a dot ...
    (microsoft.public.windowsxp.newusers)
  • Re: Can this be written better?
    ... character, apart from the dot itself, which is permitted in a file name ... I just renamed a file by placing a space in the extension and my WindowsXP ... Pro system let me do it without complaining. ...
    (microsoft.public.vb.general.discussion)
  • Re: Phiw and Ben
    ... is there any character worth their salt aside from Dot? ... worth watching, but utterly marginalized. ... Not for a second would Dot refuse to take care of her ailing hubby at home - it goes against everything we've ever known about her character over the last 23 years. ...
    (rec.arts.tv.uk.eastenders)
  • Re: Code to check if a valid e-mail address was entered
    ... Function Valid_Email(E_Address As String) As Boolean ... ' Note that the part after the @ sign must contain at least one dot, ... ' at least one other character following it, ... Dim TString As String ...
    (microsoft.public.access.modulesdaovba)