Last active
August 18, 2020 08:40
-
-
Save Joxebus/481dcced2e85f7274eb6e15a3bb59785 to your computer and use it in GitHub Desktop.
Groovy - 07 - Manipulación de archivos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File file = new File('your-dir-here') | |
def fileNames = [] | |
file.eachFile { | |
if(it.isFile()) { | |
fileNames << it.name | |
} | |
} | |
def directories = file.list() - fileNames | |
println "Files: $fileNames" | |
println "Directories: $directories" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.io.FileType | |
File directory = new File('your-dir-here') | |
String regex = /.*\.groovy/ | |
List<String> fileNames = [] | |
List<String> dirNames = [] | |
directory.eachFileMatch(~regex) { file -> | |
fileNames << file.name | |
} | |
directory.eachFile(FileType.DIRECTORIES) { dir -> | |
dirNames << dir.name | |
} | |
println "Files: $fileNames" | |
println "Directories: $dirNames" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File rfc = new File('path-of-rfc-file.txt') | |
List lines = [1,2,3,4] // the line numbers | |
String[] rfcLines = rfc as String[] | |
lines.each { line -> | |
println "$line - ${rfcLines[line]}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File dir = new File('your-dir-here') | |
String regex = /.*\.txt/ | |
String word = "word" | |
dir.eachFileMatch(~regex) { file -> | |
int count = 0 | |
List lineNumbers = [] | |
file.eachLine { line -> | |
if( line.contains(word) ) { | |
lineNumbers << count | |
} | |
count++ | |
} | |
if(lineNumbers) { | |
println "File: ${file.name} contains [$word] at lines $lineNumbers" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment