本文共 1305 字,大约阅读时间需要 4 分钟。
前一段时间从网络上下载了 Java.6.Platform.Revealed.Jul.2006.这本书, 现在JDK 6已经发布了, 就来看看里面都有什么新东西吧 . 今天就开始谈谈JDK6中的东东, 看看有那些东西可以让我们激动一下. 先来看看这个位于java.awt 包中的Desktoop类吧. 看名字就可以猜到是干什么的.
Desktop中有一些Action来支持对文件或者URI的BROWSE, EDIT, MAIL, OPEN, and PRINT操作. 记得以前为了使用java调用浏览器打开个URL要写很多代码. 现在有了这个Desktop一切就容易多了. 看看他的文档 , 就那么几个方法, 不多,但是都很常用. 不是吗, 下面来看一个例子:
import java.awt.*;import java.io.*;import java.net.*;public class DesktopTest { public static void main(String args[]) { if (!Desktop.isDesktopSupported()) { System.err.println("Desktop not supported!"); System.exit(-1); } Desktop desktop = Desktop.getDesktop(); String path; if (args.length == 0) { path = "."; } else { path = args[0]; } try { File fi = new File("test.txt"); desktop.print(fi); desktop.browse(new URI("http://blog.matrix.org.cn/icess")); } catch (Exception ioe) { System.out.println(ioe); } File dir = new File(path); File files[] = dir.listFiles(); for (File file: files) { System.out.println("Open " + file.getName() + "? [YES/NO] :"); if (desktop.isSupported(Desktop.Action.OPEN)) { String line = System.console().readLine(); if ("YES".equals(line)) { System.out.println("Opening... " + file.getName()); try { desktop.open(file); } catch (IOException ioe) { System.out.println(ioe); System.err.println("Unable to open: " + file.getName()); } } } } }}
转载地址:https://blog.csdn.net/iteye_10746/article/details/81418583 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!