How to get the correct file file creation date?

I do not need the time of the last modification, not the time of the last access to the file, but the time of creating the file. I did not find information about this. Maybe some libraries?

Path p = Paths.get(f.getAbsoluteFile().toURI()); BasicFileAttributes view = null; try { view = Files.getFileAttributeView(p, BasicFileAttributeView.class).readAttributes(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } FileTime creationTime = view.creationTime(); 

In this code, the creation time is invalid and returns today's date.

Operating System: Windows 7 Java: SE-1.7

+7
java file
source share
3 answers

As Ishavit said, not all operating systems record the created date. However, you can use java.nio.file to determine this information on operating systems that have this functionality - see the documentation for files.getAttribute - note that BasicFileAttributeView has a field for creationTime .

You can use FileSystems.getDefault(); to determine which FileAttributeView supported on the current operating system.

Files.getAttribute(path, "basic:createdAt"); will return a FileTime object with the file creation date on a system that supports BasicFileAttributeView . You will have to convert it to a java.util.Date object, but I will let you figure it out on my own.

additional literature

  • NIO API for getAttribute()
  • NIO API for BasicFileAttributeView
  • A tutorial for using readAttributes()
  • Comprehensive tutorial when using FileAttributes
  • Another StackOverflow thread in the same thread
+8
source share

How to get file creation date in Java using the BasicFileAttributes class, this is an example:

  Path path = Paths.get("C:\\Users\\jorgesys\\workspaceJava\\myfile.txt"); BasicFileAttributes attr; try { attr = Files.readAttributes(path, BasicFileAttributes.class); System.out.println("Creation date: " + attr.creationTime()); } catch (IOException e) { System.out.println("oops error! " + e.getMessage()); } 
+5
source share

You cannot do this on all systems, because not all systems even record this information. Linux, for example, does not work; see this SO stream .

Many programs "modify" a file by copying it, making changes to the copy, and then moving the copy to the original location of the file. Thus, for them there is no significant difference between creation and the last modification.

+3
source share

All Articles