programming-examples/java/Core_Java/Empty Directory.java

29 lines
924 B
Java
Raw Normal View History

2019-11-15 12:59:38 +01:00
Empty Directory
import java.io.*;
//DANGEROUS Program to empty a directory
public class Empty {
public static void main(String[] argv) {
if (argv.length != 1) { // no progname in argv[0]
System.err.println("usage: Empty dirname");
System.exit(1);
}
File dir = new File(argv[0]);
if (!dir.exists()) {
System.out.println(argv[0] + " does not exist");
return;
}
String[] info = dir.list();
for (int i=0; i File n = new File(argv[0] + dir.separator + info[i]);
if (!n.isFile()) // skip ., .., other directories too
continue;
System.out.println("removing " + n.getPath());
if (!n.delete())
System.err.println("Couldn't remove " + n.getPath());
}
}
}