Solved

delete file from folder

Posted on 2014-10-08
8
148 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 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

18 Experts available now in Live!

Get 1:1 Help Now