【问题标题】:get list of sub-directories name present under directory获取目录下存在的子目录名称列表
【发布时间】:2019-12-10 10:07:23
【问题描述】:

我对 scala 非常陌生,并试图获取存在于特定路径的子目录名称。

Directory path = "/src/test/output/"

Sub-directories present under Directory path are :20180101,20190302,19990409,20110402

我只需要 scala 中的子目录名称作为 List 。

我试过了

val result = new JFile(path).listFiles.map(_.getName).toList

但这不起作用,有人可以帮我吗??

【问题讨论】:

  • 嗨,欢迎来到 StackOverflow。请提供有关您的问题的更多详细信息,例如编译错误、错误堆栈跟踪等。
  • 我越来越空白:这是输出 (List()),不知道为什么会这样

标签: scala list apache-spark


【解决方案1】:

你可以查看here的例子。

def getListOfFiles(dir: String):List[String] = {
  val d = new File(dir)
  if (d.exists && d.isDirectory) {
    d.listFiles.filter(_.getName).toList
  } else {
    List[String]()
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-11
    • 2017-03-31
    • 2015-09-12
    • 1970-01-01
    • 2011-12-08
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多