Java FileCleaningTracker-class And Method Code Example


Here's an example of how to use the FileCleaningTracker class from the org.apache.commons.io package in Java to schedule the deletion of multiple files when the JVM exits:

import org.apache.commons.io.FileCleaningTracker;
import java.io.File;

public class Main {
    public static void main(String[] args) {
        FileCleaningTracker tracker = new FileCleaningTracker();
        File file1 = new File("path/to/file1");
        File file2 = new File("path/to/file2");
        File file3 = new File("path/to/file3");

        tracker.track(file1, "file1");
        tracker.track(file2, "file2");
        tracker.track(file3, "file3");
    }
}

This example shows how to use the FileCleaningTracker class to schedule the deletion of multiple files when the JVM exits. The track() method is used to register the files with the tracker. The second argument is a reference, which can be any string, and it is used for logging purpose,

It's important to note that FileCleaningTracker is deprecated as of commons-io 2.6, and it was removed in version 2.8, you can use File.deleteOnExit() instead which is part of the JDK.