Get only the file names using listFiles in Scala
Is there an idiomatic Scala solution to obtain only the file names from File.listFiles
?
Perhaps something like:
val names = new File(dir).listFiles.somemagicTrait(_getName)
and have names
beco开发者_如何学Gome a List[String]
?
I know I can just loop and add them to a mutable list.
how about?
new File(dir).listFiles.map(_.getName).toList
I'm always wary of answering the wrong part of the question, but as Jean-Phillipe commented, you can get an array of the names from
new File(dir).list
and if you really need a list call toList
on that.
精彩评论