Tuesday 26 June 2018

Join Variable Numbers of Paths in Groovy

Cited from Joining Paths in Java

Converts a path string, or a sequence of strings that when joined form a path string, to a Path.
...
Path representing an empty path is returned if first is the empty string and more does not contain any non-empty strings.

In Groovy, to print a variable number of arguments, the code is below.

import java.nio.file.*

String joinPath(String first, String... args){
Path path = Paths.get(first, args)
path.toString()
}

println joinPath('a')
println joinPath('a', 'b', 'c')
println joinPath('~/Data')

No comments:

Post a Comment