Joe
Joe

Reputation: 8042

How to store files on disk and guarantee that they get deleted in Java

I need to store files that a user uploads. I was instructed that I am to store it on the disk (and not try anything fancy like using database blobs or a cache system).

But from there, I need to figure out how to make sure that the files are eventually deleted (maybe an hour after they are last accessed). This is on a Linux server. So what is the best way to guarantee that the files are deleted?

Upvotes: 0

Views: 200

Answers (6)

kostja
kostja

Reputation: 61548

You can use temp files. You can define if they should get deleted on VM termination :

public static File createTempFile(String name, String suffix) {
    try {
        File temp = File.createTempFile(name, "." + suffix);

        temp.deleteOnExit();
        return temp;

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
} 

However there is no strong gurantee, as the deletion will only succeed during a regular termination.

Upvotes: 1

David Webb
David Webb

Reputation: 193716

The simplest way would be some process which periodically examined the uploads directory and deleted every file that was more than an hour old.

If you do it this way you don't need to worry about things like keeping track of files across server restarts.

Could be something like this:

File uploads =  new File("/tmp/uploads");

FileFilter hourOld = new FileFilter() {
  @Override
  public boolean accept(File file) {
    return file.lastModified() < System.currentTimeMillis() - 60 * 60 * 1000;
  }
};

for (File file : uploads.listFiles(hourOld)) {
  file.delete();
}

You could schedule this using the ScheduledExecutorService.

Upvotes: 1

Samuel Edwin Ward
Samuel Edwin Ward

Reputation: 6675

The tmpwatch program is designed to perform this exact task. Note that it won't be able to work correctly if you have the filesystem mounted with the noatime option.

Upvotes: 1

Rahul Borkar
Rahul Borkar

Reputation: 2762

I would prefer using cron4j, you can find examples here http://www.sauronsoftware.it/projects/cron4j/manual.php?PHPSESSID=oakn2j011nkoeoqv791pfcs4q3

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328614

There are many ways to do this:

  • You can run a cron job with find $path -daystart -mtime +1 -exec rm "{}" \; - that deletes all files created on day ago.

  • Whenever a file is uploaded, you can list the folder in Java and delete all files which are too old

  • You can add Quartz to your app to combine the two options.

Since you're on Linux, file locking won't be an issue. You can always delete files on Linux, even when a process is still using them (Linux will keep the file around as long as the process uses it and delete it afterwards).

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533530

To guarantee deletion, I would run a separate process which starts as a service on start-up to remove files. This way they will be deleted even if your program is not running.

Usually, all you need to to ensure files are deleted when a service is running. You just have to ensure the file is closed, and you have a background thread which checks the last accessed time periodically.

Upvotes: 1

Related Questions