Java get File Timestamp

The following example shows the modification date and time of the specified file. The class Runtime provides the runtime environment.

Java get File Timestamp

The following example shows the modification date and time of the specified file. The class Runtime provides the runtime environment.

Java get File Timestamp

Java get File Timestamp

     

In this section, you will study how to obtain the file timestamp. 

The following example shows the modification date and time of the specified file. The class Runtime provides the runtime environment. The class BufferedReader reads the file from the console and the method runtime.exec("cmd/c dir" +fname) executes the process. We have create another instance of class BufferedReader to get output from process.

 

 

Here is the code of GetFileTimestamp.java

import java.io.*;
import java.lang.*;
 
public class GetFileTimestamp {
 public static void main (String args[]){
  try {
 Runtime runtime = Runtime.getRuntime();
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Enter filename: ");
 String fname=(String)br.readLine();
 Process output = runtime.exec("cmd /c dir "+fname);
 BufferedReader bufferedReader = new BufferedReader (new 
  
InputStreamReader(output.getInputStream()));
 String out="";
 String line = null;
 
 int step=1;
  while((line = bufferedReader.readLine()) != null ) {
  if(step==6){
 out=line;
 }
  step++;
  
 try{
 out=out.replaceAll(" ","");
 System.out.println("Modification Date: "+out.substring(0,10));
 System.out.println("Modification Time: "+out.substring(10,16)+"m");
 }
 catch(Exception se){
 System.out.println("File not found");
 }
 }
 catch(Exception e){}
  }
}

Output will be displayed as:

Download Source Code