Advertisement
asurkis

Untitled

Sep 18th, 2023
1,424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.75 KB | None | 0 0
  1. import java.io.File
  2.  
  3. fun grepFile(file: File, pattern: Regex) {
  4.     file.forEachLine {
  5.         if (pattern.matches(it)) {
  6.             println(it)
  7.         }
  8.     }
  9. }
  10.  
  11. fun grepFile(path: String, pattern: String) {
  12.     grepFile(File(path), Regex(pattern))
  13. }
  14.  
  15. fun grepFiles(root: File, suffix: String, pattern: Regex) {
  16.     if (root.isFile && root.name.endsWith(suffix)) {
  17.         grepFile(root, pattern)
  18.         return
  19.     }
  20.     if (root.isDirectory) {
  21.         for (f in root.listFiles()) {
  22.             grepFiles(f, suffix, pattern)
  23.         }
  24.     }
  25. }
  26.  
  27. fun grepFiles(rootPath: String, suffix: String, pattern: String) {
  28.     grepFiles(File(rootPath), suffix, Regex(pattern))
  29. }
  30.  
  31. fun main(args: Array<String>) {
  32.     grepFiles("src", ".kt", "\\{");
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement