Please send questions to st10@humboldt.edu .
/**
/   modified from "Java Examples in a Nutshell", FIRST EDITION,
/   Flanagan, O'Reilly
/   p. 156 (pp. 48-49 in SECOND edition)
/
/   modified by: Sharon M. Tuttle
/   last modified: 10-27-00 (it works! 8-) )
**/

// remember, this package includes file i/o goodies
import java.io.*;

/**
/   "This class is a static method delete() and a standalone program
/    that deletes a specified file or directory"
**/

public class Delete
{
	/**
	/   "This is the main() method of the standalone program. After
	/    checking its arguments, it invokes the Delete.delete() method
	/    to do the deletion"
	**/

	public static void main(String[] args)
	{
		// "Check command-line arguments"
		if ((args == null) || (args.length != 1))
		{
			// very UNIX-like! printing error messages to stderr, having
			// stderr, stdin, stdout streams...
			System.err.println("Usage: java Delete ");

			// leave the program now --- will not go any further
			System.exit(0);
		}

		// (no "else" is needed here, because you can only get here
		// if args.length == 1 --- otherwise, the if statement would
		// have exited the program...)
		
		// "Call delete() and display any error messages it throws"
		try
		{
			delete(args[0]);
		}
		catch (IllegalArgumentException exc)
		{
			// there's a method getMessage() for IllegalArgumentException
			// that lets you get the associated message? useful...
			System.err.println (exc.getMessage());
		}
	}

	/**
	/   "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."
	/
	/   (note the "throws" clause in the first line of the method definition)
	**/

	public static void delete(String filename) throws IllegalArgumentException
	{
		// File object f will represent the filename parameter
		File 		f;
		
		String[] 	directoryFiles;		// if filename is a directory,
							// its contents
		boolean 	success;		// if delete is attempted,
							// did it succeed?
		
		// "Create a File object to represent the filename"
		f = new File(filename);

		// "Make sure the file or directory exists and isn't write protected"

		// (method exists() returns true if File f corresponds to a file
		// with name filename that exists)
		if (!f.exists())
		{
			// how to THROW an exception!
			throw new IllegalArgumentException(
				"Delete: no such file or directory: " + filename);
		}

		// (method canWrite() returns true if File f corresponds to a file
		// with name filename that can be written)
		if (!f.canWrite())
		{
			throw new IllegalArgumentException(
				"Delete: write protected: " + filename);
		}

		// "If it is a directory, make sure that it is empty"
		// (method isDirectory() returns true if File f corresponds to
		// a file with name filename that is a directory)
		if (f.isDirectory())
		{
			// (method list() returns an array of String's. This array
			// of String's contains the names of files in the directory
			// corresponding to the filename associated with File f)
			directoryFiles = f.list();

			// we are not allowed to remove a directory file unless
			// it is empty...
			if (directoryFiles.length > 0)
			{
				throw new IllegalArgumentException(
					"Delete: directory not empty: " + filename);
			}
		}

		// "If we passed all the tests, then attempt to delete it"
		// (method delete() tries to delete the file corresponding
		// to the filename associated with File f. It returns true
		// if successful.)
		success = f.delete();
		if (!success)
		{
			throw new IllegalArgumentException(
				"Delete: deletion of: " + filename + " failed");
		}
	}
}