简体   繁体   English

如何查询Scala的Stream [java.io.File]中存在的数据?

[英]How to query the data present in Stream[java.io.File] in Scala?

Using Scala I am trying to list all the files in a directory and then read the file attributes. 我试图使用Scala列出目录中的所有文件,然后读取文件属性。 In my case, I am trying to read the file attribute: creation date. 就我而言,我试图读取文件属性:创建日期。 The files are present in local and after doing some research, I was able to list all the files in a directory in Linux using the below code: 这些文件存在于本地,经过一些研究,我能够使用以下代码列出Linux目录中的所有文件:

import java.io.File
import scala.collection.JavaConversions._

def getFileTree(f: File): Stream[File] =
        f #:: (if (f.isDirectory) f.listFiles().toStream.flatMap(getFileTree) 
               else Stream.empty)

val filesList = getFileTree(new File("/tmp/hive_audits/"))

fileList is created in the following format: fileList以以下格式创建:

files: Stream[java.io.File] = Stream(/tmp/hive_audits, ?)

I understood that it is in the form of a collection. 我知道这是收藏的形式。 But I unable to understand how to query the 'fileList', like iterate thru the files present in the directory and then get the creation date of it. 但是我无法理解如何查询“ fileList”,就像遍历目录中存在的文件然后获取其创建日期一样。 Could anyone let me know how can I do it ? 谁能让我知道该怎么办?

You can iterate through with .foreach also with for (file <- files) {...} : 您也可以使用.foreach遍历for (file <- files) {...}

 for (file <- files) println(new java.util.Date(file.lastModified))

The java.io.File API does not support creation date. java.io.File API不支持创建日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM