JDK 6.0 新特性---Desktop

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;

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("c:/1.txt");
   desktop.print(fi);
   desktop.browse(new URI("http://www.neu.edu.cn"));
  } 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());
     }
    }
   }
  }
 }
}

 

运行结果是:打开1.txt得文件,同时用默认浏览器打开了http://www.neu.edu.cn地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值