Does groovy have an easy way to get a filename without the extension?

前端 未结 10 1035
-上瘾入骨i
-上瘾入骨i 2020-12-14 14:41

Say I have something like this:

new File(\"test\").eachFile() { file->  
println file.getName()  
}

This prints the full filename of eve

10条回答
  •  感情败类
    2020-12-14 15:24

    // Create an instance of a file (note the path is several levels deep)
    File file = new File('/tmp/whatever/certificate.crt')
    
    // To get the single fileName without the path (but with EXTENSION! so not answering the question of the author. Sorry for that...)
    String fileName = file.parentFile.toURI().relativize(file.toURI()).getPath()
    

提交回复
热议问题