Share on Google+Share on Google+

Java get File Timestamp

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

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

Advertisements

Advertisement

Posted on: November 6, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Java get File Timestamp   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
jay g shah
September 17, 2012
java

how to select path of file????????????