Here you can find the source of deleteFiles(String directory)
Parameter | Description |
---|---|
directory | a parameter |
Parameter | Description |
---|---|
Exception | an exception |
public static void deleteFiles(String directory) throws Exception
//package com.java2s; import java.io.File; public class Main { /**//from www . j a v a 2 s . com * Deletes all files in the given directory with the exception of the * given file name identified in dontDelete. The dontDelete string * should only contain a filename that is relative to the screen capture * directory, in other words this should not be an absolute path names. * @param directory * @param dontDelete Filename that is excluded from deletion * @throws Exception */ public static void deleteFiles(String directory, String dontDelete) throws Exception { File dir = new File(directory); String[] list = dir.list(); if (list.length > 0) { for (int i = 0; i < list.length; i++) { File file = new File(directory, list[i]); if (!file.getPath().equalsIgnoreCase(dontDelete)) { file.delete(); } } } } /** * Deletes all files in this directory with the exception of the * file names given in the dontDelete array. The dontDelete array * should only contain filename that are relative to the screen capture * directory, in other words there should not be absolute path names. * @param directory * @param dontDelete String array of filenames that are excluded from deletion * @throws Exception */ public static void deleteFiles(String directory, String[] dontDelete) throws Exception { File dir = new File(directory); String[] list = dir.list(); if (dontDelete == null) { // If no skip files were given, invoke normal delete deleteFiles(directory); } else if (dontDelete.length == 1 && dontDelete[0] != null) { // If there is only one non-null dontDelete, send to proper function deleteFiles(directory, dontDelete[0]); } else if (list.length > 0) { // Delete all file that are not included in dontDelete array for (int i = 0; i < list.length; i++) { File file = new File(directory, list[i]); boolean deleteFile = true; // Check if current file is in the dontDelete for (String skipFile : dontDelete) { if (file.getAbsolutePath().equals(skipFile)) { deleteFile = false; break; } } if (deleteFile) { file.delete(); } } } } /** * Deletes all files located in this directory. * @param directory * @throws Exception */ public static void deleteFiles(String directory) throws Exception { File dir = new File(directory); String[] list = dir.list(); if (list.length > 0) { for (int i = 0; i < list.length; i++) { File file = new File(directory, list[i]); file.delete(); } } } }