Is it possible to do a equivalent 'find . -ctime n' (Unix command) in JDK7? i.e. find all files based on last changed time? I had a look at the new FileVisitor/开发者_Python百科BasicFileAttributes/SimpleFileVisitor classes but I cannot see how it could be done.
The following worked for me (using Files.walkFileTree and a FileVisitor) :
FileTime ctime = (FileTime) Files.getAttribute(path, "unix:ctime");
In the JDK 7 Forums there is discussion opened on the subject
It basically says:
From creationTime's description "If the file system implementation does not support a time stamp to indicate the time when the file was created then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z)". So the behavior you observe is expected. The time of last status change is available if you really need it, try Files.getAttribute(path, "unix:ctime").
So, your own answer seems to be the right one.
You can get the creation time of a file by calling getCreationTime()
on its file attributes object. You can do a directory tree walk using Files.walkFileTree
and a FileVisitor
. Put these together and you can implement find . -ctime n
.
Here's what creationTime's javadoc says:
If the file system implementation does not support a time stamp to indicate the time when the file was created then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).
As the creation is not typical on Unix/Linux then the method is returning the last modified time.
精彩评论