Java Reference
In-Depth Information
permission on the directory it's in, nor to remove a directory that isn't empty. Here is a ver-
sion of Delete with error checking (and reporting of success, too):
public
public class
class Delete2
Delete2 {
public
public static
static void
void main ( String [] argv ) {
for
for ( String a : argv ) {
delete ( a );
}
}
public
public static
static void
void delete ( String fileName ) {
try
try {
// Construct a File object for the file to be deleted.
File target = new
new File ( fileName );
iif (! target . exists ()) {
System . err . println ( "File " + fileName +
" not present to begin with!" );
return
return ;
}
// Now, delete it:
iif ( target . delete ())
System . err . println ( "** Deleted " + fileName + " **" );
else
System . err . println ( "Failed to delete " + fileName );
} catch
catch ( SecurityException e ) {
System . err . println ( "Unable to delete " + fileName +
"(" + e . getMessage () + ")" );
}
}
}
Running it looks something like this:
$ ls -ld ?
-rw-r--r-- 1 ian ian 0 Oct 8 16:50 a
drwxr-xr-x 2 ian ian 512 Oct 8 16:50 b
drwxr-xr-x 3 ian ian 512 Oct 8 16:50 c
$ java dir_file.Delete2 ?
**Deleted** a
**Deleted** b
Search WWH ::




Custom Search