Monday, December 2, 2013

Rename the name of the file with current system date by using java program

                      File is using to store the data permanently on disk in computer. We give names to individual files. Creating file, saving the file and renaming the file on GUI system very easy but programmatically we write some code and today our topic is this one. See the below program for the answer.   


import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateAsFileName {
public static void main(String[] args) throws IOException {
         // create file object
         File file=new File("G:/text.txt");
         //create DateFormat object 
        file.createNewFile();
         
         //create calendar class object
         Calendar calendar = Calendar.getInstance();
         Date ss = calendar.getTime();
         int day = calendar.get(calendar.DAY_OF_MONTH); 
         int month = calendar.get(calendar.MONTH);
         int year = calendar.get(calendar.YEAR);
         String s="G:/"+day+"-"+month+"-"+year+".txt";
        file.renameTo(new File(s));
}

}
        

No comments:

Post a Comment