How can I access the name of the file in below scala code :
object FileMatcher {
private def filesHere = (new java.io.File("c:\\")).listFiles
def filesEnding(query: String) =
for (file <- filesHere; if file.getName.endsWith(query))
yield file.getName
def main(args: Array[String]) {
println(filesEnding(".js"))
}
}
I have one .js file and the output is : [Ljava.io.File;@df8f5e
I have tried changing ‘yield file’ to ‘yield file.getName’ but same result.
I’m assuming println(filesEnding(“.js”)) calls the toString method of whatever the def ‘filesEnding’ yields, is this correct ?
filesEndingyields anArray[String](when you useyield file.getNameandArray[File]when you useyield file) thetoStringmethod of Array, is the defaulttoStringimplementation. which is the hash code printed… and this is exactly what you’re getting. you probably meantprintln(filesEnding(".js").mkString("\t","\n\t","\n"))which should print your files nicely.