Monday, 25 April 2016

How to get the file creation date ?


There are no proper way to get the file creation date in Java, but we can use the following workaround to get the file creation date 

Command to get File creation date (Windows)
C:\>cmd /c dir c:\logfile.log /tc

Java code example
// Execute command using Windows shell
Process proc = 
     Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc");

// Get stream after command execution
BufferedReader br = new BufferedReader(
                      new InputStreamReader(proc.getInputStream()));
// Iterate till line where creation date displayed
String data = "";
for(int i=0; i<6; i++){
  data = br.readLine();
}
System.out.println("Extracted value : " + data);

// Split the line
StringTokenizer st = new StringTokenizer(data);
String date = st.nextToken();   // Get date
String time = st.nextToken();   // Get time
System.out.println("Creation Date : " + date);
System.out.println("Creation Time : " + time);

No comments:

Post a Comment

Note: only a member of this blog may post a comment.