First Version, use of File Class:
class DocWalkFile(file: File) {
def children = new Iterable[File] {
def elements =
if (file.isDirectory) file.listFiles.elements else Iterator.empty;
}
def andTree : Iterable[File] = (
Seq.single(file) ++ children.flatMap(child => new DocWalkFile(child).andTree))
}
def listDocuments(dir: File): List[File] =
(new DocWalkFile(dir)).andTree.toList filter (f => (f.getName.endsWith(".java") || f.getName.endsWith(".txt")))
Second Version:
Resources:
import java.io.File
object DocWalkFile {
def andTree(dir: File): List[File] = {
val fileList = dir.listFiles.toList
(fileList filter(f => f.isFile && f.getName.endsWith(".txt"))) :::
(fileList filter(f => f.isDirectory) flatMap (f => andTree(f) ))
}
def main(args: Array[String]): Unit = {
if (args length == 0)
println("usage: DocWalkFile [dir]")
else {
println("using dir " + args.apply(0));
andTree(new
File(args.apply(0))).foreach(f => println(">> " + f))
}
}
}
No comments:
Post a Comment