Java tutorial
//package com.java2s; import java.io.File; public class Main { protected static void delFiles(File file, String keep) { if (file.isDirectory()) { File[] files = file.listFiles(); if (files != null) { for (int i = 0, size = files.length; i < size; i++) { File f = files[i]; delFiles(f, keep); } } } else { if (keep.equals(file.getPath())) return; file.delete(); } } protected static void delFiles(File file) { if (file.isDirectory()) { File[] files = file.listFiles(); if (files != null) { for (int i = 0, size = files.length; i < size; i++) { File f = files[i]; delFiles(f); } } } else { file.delete(); } } }