Monday, September 11, 2006

File creation date and Java

Sometimes u give such weird replies to questions. A friend came to me and asked "Whats the easiest way to find the file creation date in Java" I looked at him and answered "Go check the File class, they will surely have some API". He grinned and said, "If they had, would i have asked you?". Its only then i realized there isnt a default API to get the creation time of a file in Java although u do have an API for file modification time. That got me into the thinking mode and i wrote a small code to get the creation time. All i did was to exec the command prompt and fire dir 'filename' /tc on the 'exec'ed process. The output was captured thru an inputStream and corresponding line tokenized to retrieve the date and time. Not a very neat way BUT it is one way ;) It was a small code but it made me realize - Never assume Java being a HLL does everything for you :P

5 comments:

Pappul said...

if u want a problem of the same kind AND if u r interested in UI, try making a JTree and then try and get the tool tips working for each node of the tree. The tooltip should show the name of the node .... majaa aayega karneko ....

Anonymous said...

They have a bug logged for this functionality in Java. Have a look here:

http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4122085

~Murtaza

Anonymous said...

It is not a bug or problem. Its simply because different file systems have different file attributes.

On linux (ext fs) you can only get the last access & modified dates.

So they have cleverly opted not to put it in the standard API.

Infact even on windows file is useful if we have to detect if any new file has been created in the directory but is useless for folder synchronizing programs for the simple fact that when I copy a pretty old file from one folder to another folder, ideally you expect to retain the file creation date & last access date are set to current date but the modification date remains same (modified date < file creation date :)). But if I had moved the file then the creation & modified date remein same and only last access date gets updated.

Bjørn said...

Thank you for showing me a way to solve this problem.
I want to find out when a MPEG video file is created. On windows the information is located as create date.

Bjørn

Anonymous said...

Looks like a lot of people have had a similar problem with JAVA, so now it has come a updated nio package in JAVA 7, and that has the ability to find the creation date, sample code below ...


import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.attribute.Attributes;

public class Test {
public static void main(String[] args) {

BasicFileAttributes attributes;
try {
FileSystem fs = FileSystems.getDefault();
FileRef fileRef = (FileRef) fs.getPath("/home/directv/output/april/DATA_SIMILAR.tribuneId");
System.out.println("looking up basic attributes for file {}"+ fileRef);

attributes = (BasicFileAttributes) Attributes.readBasicFileAttributes(fileRef);
System.out.println("isDirectory: {}"+ attributes.isDirectory());
System.out.println("isOther: {}"+ attributes.isOther());
System.out.println("isRegularFile: {}"+ attributes.isRegularFile());
System.out.println("isSymbolicLink: {}"+ attributes.isSymbolicLink());
System.out.println("creationTime: {}"+ attributes.creationTime());
System.out.println("lastAccessTime: {}"+ attributes.lastAccessTime());
System.out.println("lastModifiedTime: {}"+ attributes.lastModifiedTime());
} catch (IOException e) {
e.printStackTrace();
}

}
}