开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜