1

I have a .jar file called install.jar, which copies various file types to %appdata%/folder/abc/

What I need is a way to delete the files, either with the same jar or a new one, so I can reset the application for an update. I have looked on SO as well as google, and have found no answer.

If java doesn't let you delete folders, I need a way to delete all files inside of a folder, or at the very least, rename the folder.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Azulflame
  • 1,534
  • 2
  • 15
  • 30
  • You mean you haven't found anything on SO that explains how to delete a folder? http://stackoverflow.com/search?tab=relevance&q=[java]%20%2bdelete%20%2bfolder – assylias Sep 14 '12 at 16:35
  • possible duplicate of [deleting folder from java](http://stackoverflow.com/questions/3775694/deleting-folder-from-java) – assylias Sep 14 '12 at 16:38
  • If this is an application self-updater under windows then the file-locking of windows os may get in the way unless you stop the process first and ensure the updater jar is in a separate unrelated folder... Otherwise this question is a duplicate – Stephen Connolly Sep 14 '12 at 17:25

2 Answers2

4
// Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    // The directory is now empty so delete it
    return dir.delete();
}
Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
0

You could locate some useful information in this link. Which was previously asked.

Visit < Is there a quick way to delete a file from a Jar / war without having to extract the jar and recreate it?>

Community
  • 1
  • 1