Solved

delete file from folder

Posted on 2014-10-08
8
154 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
recursion example 16 126
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 24
print map entry 34 68
couple of eclipse 5 36
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

770 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