简体   繁体   English

如何提取 groovy 中的文件属性?

[英]How to extract file properties in groovy?

I have gsp which has table and i need to display created date time and last modified time of each file which in drive.我有具有表格的 gsp,我需要显示驱动器中每个文件的创建日期时间和最后修改时间。

I am not getting how to retrieve file properties.can any body answer me.我不知道如何检索文件属性。任何人都可以回答我。

Advance thanks laxmi.P提前感谢 laxmi.P

The result of file.lastModified() is a long we can use to construct a new Date object. file.lastModified() 的结果是一个 long,我们可以用它来构造一个新的 Date object。 We can apply formatting on the Date object.我们可以在日期 object 上应用格式。 The formatting rules of SimpleDateFormat can be applied.可以应用 SimpleDateFormat 的格式化规则。

new File('.').eachFileRecurse { file ->
    println new Date(file.lastModified()).format('EEE MMM dd hh:mm:ss a yyyy')
}

You probably want something like:你可能想要这样的东西:

new File(path-to-your-directory).eachFileRecurse{file->
println file.lastModified()
}

To get access to properties not supported by the Java File API we can parse the output of a 'dir' or 'ls' command:要访问 Java 文件 API 不支持的属性,我们可以解析“dir”或“ls”命令的 output:

def file = 'sample.txt'
def cmd = ['cmd', '/c', 'dir', file, '/tc'].execute()
cmd.in.eachLine { line ->
    if (line.contains(file)) {
        def created = line.split()[0]
        println "$file is created on $created"
    }
} 

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

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