Is there an easy way to discover a File\'s creation time with Java? The File class only has a method to get the \"last modified\" time. According to some resources I found
This is a basic example in Java
, using BasicFileAttributes class:
Path path = Paths.get("C:\\Users\\jorgesys\\workspaceJava\\myfile.txt");
BasicFileAttributes attr;
try {
attr = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("File creation time: " + attr.creationTime());
} catch (IOException e) {
System.out.println("oops un error! " + e.getMessage());
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CreateDateInJava {
public static void main(String args[]) {
try {
// get runtime environment and execute child process
Runtime systemShell = Runtime.getRuntime();
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter filename: ");
String fname = (String) br1.readLine();
Process output = systemShell.exec("cmd /c dir \"" + fname + "\" /tc");
System.out.println(output);
// open reader to get output from process
BufferedReader br = new BufferedReader(new InputStreamReader(output.getInputStream()));
String out = "";
String line = null;
int step = 1;
while ((line = br.readLine()) != null) {
if (step == 6) {
out = line;
}
step++;
}
// display process output
try {
out = out.replaceAll(" ", "");
System.out.println("CreationDate: " + out.substring(0, 10));
System.out.println("CreationTime: " + out.substring(10, 16) + "m");
} catch (StringIndexOutOfBoundsException se) {
System.out.println("File not found");
}
} catch (IOException ioe) {
System.err.println(ioe);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
/**
D:\Foldername\Filename.Extension
Ex:
Enter Filename :
D:\Kamal\Test.txt
CreationDate: 02/14/2011
CreationTime: 12:59Pm
*/
I've been investigating this myself, but I need something that will work across Windows/*nix platforms.
One SO post includes some links to Posix JNI implementations.
In particular, JNA-POSIX implements methods for getting file stats with implementations for Windows, BSD, Solaris, Linux and OSX.
All in all it looks very promising, so I'll be trying it out on my own project very soon.
The javaxt-core library includes a File class that can be used to retrieve file attributes, including the creation time. Example:
javaxt.io.File file = new javaxt.io.File("/temp/file.txt");
System.out.println("Created: " + file.getCreationTime());
System.out.println("Accessed: " + file.getLastAccessTime());
System.out.println("Modified: " + file.getLastModifiedTime());
Works with Java 1.5 and up.
With the release of Java 7 there is a built-in way to do this:
Path path = Paths.get("path/to/file");
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();
It is important to note that not all operating systems provide this information. I believe in those instances this returns the mtime which is the last modified time.
Windows does provide creation time.
I like the answer on jGuru that lists the option of using JNI to get the answer. This might prove to be faster than shelling out and you may encounter other situations such as this that need to be implemented specifically for windows.
Also, if you ever need to port to a different platform, then you can port your library as well and just have it return -1 for the answer to this question on *ix.