Home Java Java-get-example Java get File Timestamp



Java get File Timestamp
Posted on: November 6, 2008 at 12:00 AM
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

Related Tags for Java get File Timestamp:
cfileideclassdateprocesstimeconsoleioruntimemethodoutputgetcmdenvironmentvibufferexecreadnameoleinstanceidexecreateshowifieexampleexecutetoexamrunciprocwssheilreaderdesnotadsputruntime.execpemodificationimfromceinnomodasstamntoutcaadclesspecdirbufferedreadermehowmodiprocatenvcutxaxampsxespsoessbufferedatirironhaimellmplfolloweaandconswingufruntsruntsrocthshocmavst.exeatifeicaicapleplprocesprndonomolo


More Tutorials from this section

Ask Questions?    Discuss: Java get File Timestamp   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.