Home > Articles > Programming > Java

Mustang (Java SE 6) Gallops into Town

Jeff Friesen
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowJeff Friesen

Jeff Friesen

Learn more…

Using Transitions to Simplify JavaFX Animations
Sep 9, 2009
Styling Swing Components with Java CSS
Aug 28, 2009
Show Me the Movie with DirectShow
Apr 28, 2009
Playing Media with Java Media Components
Feb 27, 2009
Creating Java User Interfaces with Project Scene Graph
Feb 10, 2009
Blending Images in Java
Sep 12, 2008
Introduce Animated Cursors to Java GUIs, Part 3
Apr 30, 2008
Introduce Animated Cursors to Java GUIs, Part 2
Apr 18, 2008
Introduce Animated Cursors to Java GUIs, Part 1
Apr 11, 2008
Enhance Java GUIs with Windows Icons
Apr 4, 2008
Taming Mustang, Part 2: Scripting API Tour
Nov 2, 2007
Visit Java's Relatives: Jython and Groovy
May 4, 2007
Generics 101: Mastering the Fundamentals
Mar 23, 2007
Taming Mustang, Part 3: A New Script Engine
Mar 2, 2007
Taming Mustang, Part 1: Collections API
Feb 16, 2007
PCX Meets Image I/O: Creating an Image-Reading Java Plug-in
Dec 29, 2006
Mustang (Java SE 6) Gallops into Town
Oct 20, 2006
GridBagLayout Versus FormLayout
Oct 6, 2006
Laying Out Realistic GUIs the GridBagLayout Way
Sep 29, 2006
Harness the Power of Java's GridBagLayout
Sep 22, 2006
Tools of the Trade: SwingX Meets Swing with New and Extended Components
Aug 4, 2006
Have Fun with the Custom Screensavers Library
Mar 10, 2006
Build Screensavers with a Custom Screensavers Library in Borland C++
Feb 24, 2006
Tools of the Trade: Flash meets Java with Transform SWF and JFlashPlayer
Feb 17, 2006
Tools of the Trade, Part 3: Using the JGoodies Animation Library
Dec 22, 2005
Tools of the Trade, Part 2: Building Graphs with JGraph
Dec 9, 2005
Tools of the Trade, Part 1: Creating PDF documents with iText
Nov 4, 2005
From Literals to Expressions in Java
Aug 16, 2002
Build Your Own Java-Based Email Programs
May 10, 2002
Exploring Java's Network API: URIs and URLs
May 1, 2002
Exploring Java's Network API: Sockets
Apr 19, 2002
Basic Thread Operations in Java
Mar 22, 2002
Working with Streams in Java
Mar 22, 2002
Advanced Tips for More Powerful Tables
Nov 20, 2001
Exploring Swing's Table Component
Nov 20, 2001
Simple Tips for More Powerful Tables
Nov 20, 2001
A Handful of Tips for Swing Programs
Apr 13, 2001
A Trio of Tips for AWT Programs
Apr 13, 2001
Automating Programs with Robots
Apr 13, 2001
Build Your Own Media Player
Apr 13, 2001
Drawing Cubic Curves
Apr 13, 2001
Scaling Images
Apr 13, 2001
Using the Swing API Timers
Apr 13, 2001

Sorry, this author hasn't posted any blogs.

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.

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 ThisShare This
  • Your Account

Discussions

Look and feel
Posted Jun 17, 2008 05:43 AM by pulkit.bhanot
0 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on November 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network