Solved

delete file from folder

Posted on 2014-10-08
8
151 Views
Last Modified: 2014-10-11
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?
0
Comment
Question by:Rocking
  • 5
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 40369011
If this is a server side app then you should consider using a Scheduler such as Quartz
0
 

Author Comment

by:Rocking
ID: 40369816
how to get the file time when it is copied in the folder and check it's time to delete(1 hr is over)?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40370179
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
0
 

Author Comment

by:Rocking
ID: 40371130
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");
                    }
              }
          }
      }
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 40371234
Firstly, you only need one DateFormat, not two, and it should be a final class variable
Secondly - i'm surprised that library doesn't have built in recursive routines - are you sure it doesn't?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 40371247
Update: yes, of course it does. Not only that: it has AgeFileFilter to filter by age
0
 
LVL 1

Expert Comment

by:srikotesh
ID: 40372666
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40374535
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Passing list of object to Oracle Database Procedure 3 59
groupSum6 challenge 6 76
bitbucket vs gitbucket 3 57
varialbe initialization 11 30
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now