a new class--desktop was added after Jdk1.6, which is explained in the JDK:
The Desktop class allows a Java application to launch associated applications registered on the native desktop to ha Ndle a URI or a file.
Supported operations include:
- Launching the User-default browser to show a specified URI;
- Launching the User-default mail client with an optional
mailto URI;
- Launching a registered application to open, edit or print a specified file.
The meaning of this passage is:The Desktop class allows a Java application to launch another local application to handle a URI or file request, which contains several methods:1. Start the user's default browser to display the specified URI link2. Start User default mail client send URI specified message3. Launch a registered application (locally installed application) to open, edit or print a specified file
Below, give a test code to illustrate the function of this class and how to use it, with comments in the code:
Package com.brucezhang.desktop; Import Java.awt.Desktop; Import Java.io.File; Import Java.net.URI; public class Desktoptest {private static desktop desktop; Open Web page using default browser public static void browse () {if (desktop.isdesktopsupported ()) {des Ktop = Desktop.getdesktop (); try {//uri Specifies the address of the Web page desktop.browse (The New URI ("Http://blog.csdn.net/dlutbrucezhang?v Iewmode=contents ")); } catch (Exception e) {//Todo:handle Exception e.printstacktrace (); }}}//edit file public static void edit () {if (deskto P.isdesktopsupported ()) {desktop = Desktop.getdesktop (); try {desktop.edit (new File ("D:\\brucezhang.txt")); } catch (Exception e) {//Todo:handle exception e.printstacktrace (); }}}//Open the file, it is similar to the process of editing the file, you can see the file display public static void open () { if (desktop.isdesktopsupported ()) {Desktop = Desktop.getdesktop (); try {desktop.open (new File ("D:\\brucezhang.txt")); } catch (Exception e) {//Todo:handle Exception e.printstacktrace (); }}}//print the specified file public static void print () {if (D Esktop.isdesktopsupported ()) {desktop = Desktop.getdesktop (); try {desktop.print (new File ("D:\\brucezhang.txt")); } catch (Exception e) {//Todo:handle Exception e.printstacktrace (); }}}/** * @param args */public static void main ( String[] args) {//TODO auto-generated Method Stub browse (); Edit (); Open (); Print (); } }
Introduction to the use of desktop classes in Java