A new class--desktop has been added after Jdk1.6, which is explained in the JDK as follows:
The Desktop class allows a Java application to launch associated applications in the registered native to Desktop 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 a 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 one Java application to start another local application to process a URI or file request, which includes several methods as follows:
1. Start the user's default browser to display the specified URI link
2. Start the user's default mail client send the message specified by the URI
3. Start a registered application (locally installed application) to open, edit or print a specified file
Below, give a test code to illustrate the function and use of this class, 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 with default browser public static void browse () {if (desktop.isdesktopsupported ()) {Desktop = De
Sktop.getdesktop ();
The try {//uri Specifies the address of the Web page desktop.browse (The New URI ("Http://blog.csdn.net/dlutbrucezhang?
Viewmode=contents "));
catch (Exception e) {//Todo:handle Exception e.printstacktrace (); }}//edit file public static void edit () {if (desktop.isdesktopsupported ())
{desktop = Desktop.getdesktop ();
try {desktop.edit (new File ("D:\\brucezhang.txt")); catch (Exception e) {//Todo:handle Exception E.PRintstacktrace (); The file is opened, similar to the procedure for editing the file, to see the display of the file public static void open () {if deskt
Op.isdesktopsupported ()) {desktop = Desktop.getdesktop ();
try {desktop.open (new File ("D:\\brucezhang.txt"));
catch (Exception e) {//Todo:handle Exception e.printstacktrace (); The specified file public static void print () {if (desktop.isdesktopsupport
Ed ()) {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 (); }
}