使用.jar删除文件

时间:2012-09-14 16:33:39

标签: java file jar delete-file

我有一个名为install.jar的.jar文件,它将各种文件类型复制到%appdata%/ folder / abc /

我需要的是一种删除文件的方法,使用相同的jar或新文件,因此我可以重置应用程序以进行更新。我看过SO以及谷歌,但没有找到答案。

如果java不允许你删除文件夹,我需要一种方法来删除文件夹中的所有文件,或者至少重命名文件夹。

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

您可以在此链接中找到一些有用的信息。以前曾问过这个问题。

访问&lt; Is there a quick way to delete a file from a Jar / war without having to extract the jar and recreate it?&GT;