JDK 6 探秘之一: Desktop

前一段时间从网络上下载了 Java.6.Platform.Revealed.Jul.2006.这本书, 现在JDK 6已经发布了, 就来看看里面都有什么新东西吧 . 今天就开始谈谈JDK6中的东东, 看看有那些东西可以让我们激动一下. 先来看看这个位于java.awt 包中的Desktoop类吧. 看名字就可以猜到是干什么的.

Desktop中有一些Action来支持对文件或者URI的BROWSE, EDIT, MAIL, OPEN, and PRINT操作. 记得以前为了使用java调用浏览器打开个URL要写很多代码. 现在有了这个Desktop一切就容易多了. 看看他的文档 http://download.java.net/jdk6/docs/api/java/awt/Desktop.html  , 就那么几个方法, 不多,但是都很常用. 不是吗, 下面来看一个例子:

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());
          }
       }
     }
   }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值