Search Java Programs

Friday, February 5, 2010

Change a file timestamp

Timestamp is a utility to change or  modify the time of a file. Java provides the facility for changing a file timestamp according to the  user reliability.  
Description of program:
This program helps you in changing a file timestamp or modification time in Java. After running this program it will take a file name and its modification date in 'dd-mm-yyyy' format. Then it will check the given file is exist or not using the exists() method. When the file exists, this program will change the date of given file and it will display a message "Modification is successfully!" otherwise it will show "File does not exists!".
Description of code:
setLastModified(long time):
This is the method that sets the last modification time of a file or directory and returns Boolean types values either 'true' or 'false'. If it will  return a 'true' only
when the modification is completely successfully otherwise, it will return 'false'. This method takes following long type data:
        time: This is the time that have to be modified or set. 
getTime():
This is the method that returns the number of milliseconds in GMT format like: 23-04-2007.

Here is the code of program:
import java.io.*;
import java.util.*;

import java.text.*;


public class ChangeFileDate{
 
  public static void main(String[] args) {
 
    try{
 
     System.out.println("Change file timestamp example!");
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter file name with extension:");
 
      String str = bf.readLine();
      System.out.println("Enter last modified date in 'dd-mm-yyyy' format:");
      String strDate = bf.readLine();
 
     SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy");
 
     Date date = sdf.parse(strDate);
  
    File file = new File(str);
 
      if (file.exists()){

        file.setLastModified(date.getTime());
 
       System.out.println("Modification is successfully!");
   
   }

      else{

        System.out.println("File does not exists!");

      }
   
 }

    catch(Exception e){
 
     e.printStackTrace();

    }
  
}

}
Output of program:
C:\vinod\Math_package>javac ChangeFileDate.java

C:\vinod\Math_package>java ChangeFileDate
Change file timestamp example!
Enter file name with extension:
StrStartWith.shtml
Enter last modified date in 'dd-mm-yyyy' format:
23-04-2007
Modification is successfully!

No comments:

Post a Comment

Website Design by Mayuri Multimedia