file
[!NOTE|label:references:]
- Class File
- Class FilenameUtils)
- Package java.nio.file
- Class Paths VS. Interface Path)
// https://stackoverflow.com/a/49019436/2940319 assert sun.nio.fs.UnixPath == java.nio.file.Paths.get( '/a/b/c/d.txt' ).getClass() java.nio.file.Path path = java.nio.file.Paths.get( '/a/b/c/d.txt' )
- Java Files - java.nio.file.Files Class
IO | NIO |
---|---|
File file = new File( 'c:/data' ) file.createNewFile() |
Path path = Paths.get( 'c:/data' ) Files.createFile(path) |
File file = new File( 'c:/data' ) file.mkdir() |
Path path = Paths.get( 'c:/data' ) Files.createDirectory(path) |
File file = new File( 'c:/data' ) file.mkdirs() |
Path path = Paths.get( 'c:/data' ) Files.createDirectories(path) |
File file = new File( 'c:/data' ) file.exists() |
Path path = Paths.get( 'c:/data' ) Files.exists(path) |
dirname
// via File assert '/a/b/c' == ( new File('/a/b/c/d.txt') ).getParentFile().toString() assert '/a/b/c' == ( new File('/a/b/c/d.txt') ).getParent() assert '/a/b/c' == ( new File('/a/b/c/d.txt') ).parent // via java.nio.file.Paths assert '/a/b/c' == java.nio.file.Paths.get( '/a/b/c/d.txt' ).getParent().toString() assert '/a/b/c' == jhava.nio.file.Paths.get( '/a/b/c/d.txt' ).parent.toString()
basename
// via File assert 'd.txt' == (new File('/a/b/c/d.txt')).getName() assert 'd.txt' == (new File('/a/b/c/d.txt')).name // via java.nio.file.Paths assert 'd.txt' == java.nio.file.Paths.get( '/a/b/c/d.txt' ).getFileName().toString() assert 'd.txt' == java.nio.file.Paths.get( '/a/b/c/d.txt' ).fileName.toString()
isDirectory || isFile
assert true == ( new File('/Users/marslo/.vimrc') ).isFile() assert false == ( new File('/Users/marslo/.vimrc') ).isDirectory()
traverse
groovy.io.FileType.FILES
[!NOTE|label:references:]
#!/usr/bin/env groovy
import groovy.transform.Field
String pathToFolder = '/Users/marslo/.ssh'
new File(pathToFolder).traverse(type: groovy.io.FileType.FILES) {
println it
}
// vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=Groovy
jenkins libs
import groovy.io.FileType
import static groovy.io.FileType.*
@NonCPS
def traverseInPath( String path, String filetype, Integer depth = 1 ) {
List<String> names = []
if ( ! [ 'files', 'directories', 'any' ].contains(filetype) ) {
currentBuild.description = "`filetype` support only ${[ 'files', 'directories', 'any' ].join(',')} !"
currentBuild.result = 'NOT_BUILT'
currentBuild.getRawBuild().getExecutor().interrupt(Result.NOT_BUILT)
}
Closure sortByTypeThenName = { a, b ->
a.isFile() != b.isFile() ? a.isFile() <=> b.isFile() : a.name.toLowerCase() <=> b.name.toLowerCase()
}
new File(path).traverse(
type : FileType.valueOf( filetype.toUpperCase() ),
maxDepth : depth,
sort : sortByTypeThenName
) {
names << it
}
return names
}
jenkinsfile
String path = '/mnt/to/path/1' String path2 = '/mnt/to/path/2' node ( NODE_NAME ) { println ( ">> traverse all FILES/DIRECTORIES in ${path} in maxDepth 0 : " ) println traverseInPath( path, 'any', 0 ).join('\n') println ( ">> traverse all FILES/DIRECTORIES in ${path} in maxDepth 1 : " ) println traverseInPath( path, 'any', 1 ).join('\n') println ( ">> traverse all FILES in ${path} in maxDepth 2 : " ) println traverseInPath( path, 'files', 2 ).join('\n') println ( ">> traverse all FILES in ${path} in maxDepth 3 : " ) println traverseInPath( path, 'files', 3 ).join('\n') }