Home > Articles > Programming > Java

Mustang (Java SE 6) Gallops into Town

  • Print
  • + Share This
Mustang is galloping into town. Also known as Java SE 6, Sun's latest incarnation of the Java 2 platform should arrive in its first non-beta release by the time you read this article. Jeff Friesen shows you why the many new features (from console I/O and access permissions control methods, to the system tray API and table sorting and filtering) that you now get to play with make Mustang an especially interesting release.

Author's Note (7/23/07): This article's DesktopDemo and TableSortFilterDemo applications have been revised to create their GUIs on Swing's event-dispatching thread. This is in keeping with the latest advice offered by Sun's Java Tutorial (http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html). Also, I've also corrected a small bug in the NetParmsDemo application, where a NullPointerException is thrown because of an API problem.

Like this article? We recommend

Mustang is galloping into town. Also known as Java SE 6, Sun’s latest incarnation of the Java 2 platform should arrive in its first non-beta release by the time you read this article. The many new features (from console I/O and access permissions control methods, to the system tray API and table sorting and filtering) that we now get to play with make Mustang an especially interesting release.

Earlier this year, JavaWorld published Start saddling up for Mustang, my first article dedicated to Java SE 6. That article explored Mustang’s new console I/O API, the new partition-space methods in the java.io.File class, and the new splash-screen and system tray APIs.

This article continues my exploration of Mustang. After investigating several new File methods for controlling file and directory access permissions, the article presents the new desktop integration API. Moving on, the article examines Mustang’s new programmatic access to network parameters capability. The article closes with the table component’s new sorting and filtering capabilities.

Access Permissions Control Methods

An instance of the File class is an abstract pathname that identifies a file or directory object in the underlying file system. The file system may restrict the read, write, and execute operations that can be performed on this object.

Read, write, and execute restrictions are collectively known as access permissions. The file system may associate multiple sets of access permissions with a single object. One set may apply to the object’s owner and another set may apply to all other users, for example.

The current access permissions, as applied to the individual attempting to access the object, may cause some of File’s methods to fail. For this reason, Mustang introduces six new File methods that let you modify the pathname’s access permissions:

  • public boolean setExecutable(boolean executable, boolean ownerOnly) sets the owner’s or everyone’s execute permission for this abstract pathname. Pass true to parameter executable to allow execute operations; pass false to disallow execution. Pass true to parameter ownerOnly to allow/disallow this permission for just the abstract pathname’s owner; pass false to apply the permission to everyone. If the underlying file system doesn’t distinguish the owner’s execute permission from everyone’s execute permission, the permission applies to everyone, regardless of the ownerOnly value.

    This method returns true on success and false on failure. Failure occurs if the user doesn’t have permission to change the abstract pathname’s access permissions or if the underlying file system doesn’t implement an execute permission and executable contains false.

  • public boolean setExecutable(boolean executable) is a convenience method to set the owner’s execute permission for this abstract pathname.

  • public boolean setReadable(boolean readable, boolean ownerOnly) sets the owner’s or everyone’s read permission for this abstract pathname. Pass true to parameter readable to allow read operations; pass false to disallow read. Pass true to parameter ownerOnly to allow/disallow this permission for just the abstract pathname’s owner; pass false to apply the permission to everyone. If the underlying file system doesn’t distinguish the owner’s read permission from everyone’s read permission, the permission applies to everyone regardless of the ownerOnly value.

    This method returns true on success and false on failure. Failure occurs if the user doesn’t have permission to change the abstract pathname’s access permissions or if the underlying file system doesn’t implement a read permission and readable contains false.

  • public boolean setReadable(boolean readable) is a convenience method to set the owner’s read permission for this abstract pathname.

  • public boolean setWritable(boolean writable, boolean ownerOnly) sets the owner’s or everyone’s write permission for this abstract pathname. Pass true to parameter writable to allow write operations; pass false to disallow write. Pass true to parameter ownerOnly to allow/disallow this permission for just the abstract pathname’s owner; pass false to apply the permission to everyone. If the underlying file system doesn’t distinguish the owner’s write permission from everyone’s write permission, the permission applies to everyone regardless of the ownerOnly value.

    This method returns true on success and false on failure. Failure occurs if the user doesn’t have permission to change the abstract pathname’s access permissions.

  • public boolean setWritable(boolean writable) is a convenience method to set the owner’s write permission for this abstract pathname.

File provides counterpart methods that enable you to obtain the current settings of an object’s read, write, and execute permissions: public boolean canRead(), public boolean canWrite(), and (new in Mustang) public boolean canExecute().

I created a simple WritableDemo application that demonstrates the setWritable() and canWrite() methods. This application enables you to make a file system object writable or read-only, and it lets you view this permission’s current setting. The source code appears in Listing 1.

Listing 1 WritableDemo.java

// WritableDemo.java

import java.io.File;

public class WritableDemo
{
  public static void main (String [] args)
  {
   if (args.length < 1 || args.length > 2)
   {
     System.err.println ("usage : java WritableDemo [option] filespec");
     System.err.println ("");
     System.err.println ("options");
     System.err.println ("");
     System.err.println (" + makes the file writable");
     System.err.println (" - makes the file read-only");
     System.err.println (" no option returns file’s writable status");
     System.err.println ("");
     System.err.println ("example: java WritableDemo test.dat");
     return;
   }

   String option = (args.length == 1) ? "" : args [0];
   File filespec = new File (args [(args.length == 1) ? 0 : 1]);

   if (option.equals ("+"))
   {
     if (filespec.setWritable (true))
       System.out.println (filespec + " is now writable");
     else
       System.out.println ("Permission denied");
   }
   else
   if (option.equals ("-"))
   {
     if (filespec.setWritable (false))
       System.out.println (filespec + " is now read-only");
     else
       System.out.println ("Permission denied");
   }
   else
     System.out.println (filespec + " is " + 
               (filespec.canWrite ()
               ? "writable" : "read-only"));
  }
}
  • + Share This
  • 🔖 Save To Your Account