DELETING FILES AUTOMATICALLY CREATE A SINGLE CALENDAR AT THE BEGINNING longcutoff=System.currentTimeMillis()-(30 *24*60*60*1000);
Subtract 30 days, and convert this to a Date
and then a long, to represent the cutoff timestamp. Then just delete all files older than that timestamp, comparing longs using < or >. CODE FOR DELETING FILE publicstaticvoiddeleteFilesOlderThanNdays(intdaysBack,StringdirWay,org.apache.c ommons.logging.Loglog){
File[]listFiles=directory.listFiles(); longpurgeTime=System.currentTimeMillis()-(daysBack*24*60*60*1000); for(FilelistFile:listFiles){ if(listFile.lastModified()<purgeTime){ if(!listFile.delete()){ System.err.println("Unabletodeletefile:"+listFile); } } } }else{ log.warn("Fileswerenotdeleted,directory"+dirWay+"does'ntexist!"); } } CALENDAR OBJECT Calendarcal=Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH,daysBack* -1); longpurgeTime=cal.getTimeInMillis(); It is worthwhile printing out30 * 24 * 60 * 60 * 1000, which is anint. It will probably give an overflow error and print a negative value. You can correct that by changing it to30L * 24 * 60 * 60 * 1000. publicstaticbooleanfileMove(Filef1,Filef2,booleanoverwrite){ ReadableByteChannelsrc=null; WritableByteChanneldest=null; booleansuccess=false;