tags:

views:

45

answers:

3

Hi,

How to get file name from these lines using groovy .

File file = new File(SOURCE_FILE_NAME).eachLine{line->
    println line
}

getting line like this :

/usr/local/
/usr/local/testing.groovy
/usr/local/picture.jpg

expecting output:

testing.groovy
picture.jpg

thanks

A: 
println (new File(yourString).name)

This should work.

Juriy
Hi juriy,here my i am doing File file = new File(SOURCE_FILE_NAME).eachLine{line->println line}i was getting /usr/local/testing.groovy. now i need to get only filename excluding the path. thanks
srinath
+1  A: 

I don't know about groovy, but the regex you're looking for would be[^/]+$

Amarghosh
+1  A: 
File file = new File(SOURCE_FILE_NAME).eachLine{ path ->
    println org.apache.commons.lang.StringUtils.substringAfterLast(path, "/")
}
Don
Hi, when used ur path , got error java.lang.StringIndexOutOfBoundsException: String index out of range: 16. I also had /usr/local/ .may be with this showing error ?. thanks
srinath
thanks don, how to skip for line with out file name extension ?lines also contains /usr/local, /usr/local/test.org.groovy ....
srinath
Should be OK now
Don