Here you can find the source of deleteContents(String directory)
Parameter | Description |
---|---|
IOException | if there's a problem deleting some file |
public static void deleteContents(String directory) throws IOException
//package com.java2s; /******************************************************************************* * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/* w w w .j a v a 2 s . c om*/ * IBM Corporation - initial API and implementation *******************************************************************************/ import java.io.File; import java.io.IOException; public class Main { /** * delete all files (recursively) in a directory. This is dangerous. Use with * care. * * @throws IOException * if there's a problem deleting some file */ public static void deleteContents(String directory) throws IOException { File f = new File(directory); if (!f.exists()) { return; } if (!f.isDirectory()) { throw new IOException(directory + " is not a vaid directory"); } for (String s : f.list()) { deleteRecursively(new File(f, s)); } } private static void deleteRecursively(File f) throws IOException { if (f.isDirectory()) { for (String s : f.list()) { deleteRecursively(new File(f, s)); } } boolean b = f.delete(); if (!b) { throw new IOException("failed to delete " + f); } } }