FileDocCategorySizeDatePackage
Delete.javaAPI DocExample2425Mon Sep 22 13:30:30 BST 1997None

Delete

public class Delete extends Object
This class is a static method delete() and a standalone program that deletes a specified file or directory.

Fields Summary
Constructors Summary
Methods Summary
public static voiddelete(java.lang.String filename)
The static method that does the deletion. Invoked by main(), and designed for use by other programs as well. It first makes sure that the specified file or directory is deleteable before attempting to delete it. If there is a problem, it throws an IllegalArgumentException.

    // Create a File object to represent the filename
    File f = new File(filename);
    // Make sure the file or directory exists and isn't write protected
    if (!f.exists()) fail("Delete: no such file or directory: " + filename);
    if (!f.canWrite()) fail("Delete: write protected: " + filename);
    // If it is a directory, make sure it is empty
    if (f.isDirectory()) {
      String[] files = f.list();
      if (files.length > 0) fail("Delete: directory not empty: " + filename);
    }
    // If we passed all the tests, then attempt to delete it
    boolean success = f.delete();
    // And throw an exception if it didn't work for some (unknown) reason.
    // For example, because of a bug with Java 1.1.1 on Linux, 
    // directory deletion always fails 
    if (!success) fail("Delete: deletion failed");
  
protected static voidfail(java.lang.String msg)
A convenience method to throw an exception

    throw new IllegalArgumentException(msg);
  
public static voidmain(java.lang.String[] args)
This is the main() method of the standalone program. After checking it arguments, it invokes the Delete.delete() method to do the deletion

    if (args.length != 1) {    // Check command-line arguments
      System.err.println("Usage: java Delete <file or directory>");
      System.exit(0);
    }
    // Call delete() and display any error messages it throws.
    try { delete(args[0]); }
    catch (IllegalArgumentException e) { System.err.println(e.getMessage()); }