Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

delete file from folder

Posted on 2014-10-08
8
Medium Priority
?
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 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 2000 total points
ID: 40371247
Update: yes, of course it does. Not only that: it has AgeFileFilter to filter by age
0
 
LVL 2

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.
Suggested Courses

688 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