1

I have tried using the below code to get the file creation time of a file in linux system. But instead of giving the creation time , it is giving last modified time. Please help me to get the file creation time in java for linux file system.

File f=new File("/var/fileName.csv");
Path p = f.toPath();
BasicFileAttributes attr = Files.readAttributes(p, BasicFileAttributes.class);

System.out.println("creationTime: " + attr.creationTime().toInstant().atZone(ZoneId.systemDefault()));

Thanks in advance!!

2
  • which file system do you use exactly? Do you mean ext2/3/4? Commented Nov 21, 2018 at 12:30
  • Even if the filesystem being used actually tracks creation time (not all do), there's no easy way to get it, as you have to invoke the statx syscall manually since there's no glibc wrapper function for it. Not surprising if the jre you're using doesn't support that. Commented Nov 21, 2018 at 12:38

1 Answer 1

1

Reading the Javadoc show the following:

FileTime creationTime()

Returns the creation time. The creation time is the time that the file was created.

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 it would appear that this is an issue with the filesystem you are on, and the corresponding filesystem implementation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.