Link to home
Start Free TrialLog in
Avatar of Rocking
Rocking

asked on

delete file from folder

Hi,

I need to write java code which will copy the file from one folder to another and will deleted after one hour.
the time is dynamic say if the file copied at destination at 15:00 then it should be deleted at 16:00
or
 if the file copied at destination at 24:00 then it should be deleted at 01:00

File Copy is Ok with Apache utils,but how to achieve the later part?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

If this is a server side app then you should consider using a Scheduler such as Quartz
Avatar of Rocking
Rocking

ASKER

how to get the file time when it is copied in the folder and check it's time to delete(1 hr is over)?
how to get the file time when it is copied in the folder
Well, you'll know that, won't you? You're copying it ;)
Personally i'd name the file to include its timestamp. You could then have a Quartz job checking those names/timestamps
Avatar of Rocking

ASKER

Please suggest if we can make it better

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.IOUtils;

public class CopyFileApacheUtils {

      public static void main(String[] args) throws ParseException {
            File destination = new File("C:\\temp");
            listFilesForFolder(destination);
      }

      public static void listFilesForFolder(final File folder) throws ParseException {
          for (final File fileEntry : folder.listFiles()) {
              if (fileEntry.isDirectory()) {
                  listFilesForFolder(fileEntry);
              } else {       
                    System.out.println(fileEntry.getName());
              Date date = new Date();
                    Date d1 = null;
                    Date d2 = null;
                    SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyyHHmmss");
                    SimpleDateFormat sdf_CurrentDate = new SimpleDateFormat(
                                "ddMMyyyyHHmmss");
                    String currentDateTime = sdf.format(date);
                    String fileCreationTime = sdf.format(fileEntry.lastModified());
                    d1 = sdf.parse(currentDateTime);
                d2 = sdf.parse(fileCreationTime);
                    long diff = d1.getTime() - d2.getTime();
                    long diffHours = diff / (60 * 60 * 1000);
                    long diffMinutes = diff / (60 * 1000) % 60;
                    if (diffHours > 24) {
                          System.out.println("Time To Delete");
                          fileEntry.delete();
                    }else{
                          System.out.println("Still Alive");
                    }
              }
          }
      }
}
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
https://forums.bukkit.org/threads/useful-awesome-code-snippets.104409/
see the above url it contains info about how delete folder and how to delete the files from the folder.
once u copied the files to the destination folder
by using threads concept u can achieve once the time reached to one hour u can use the above link to delete the files.
:)