background image

最早以前,写一个文件下载的程序,判断文件的类型是个大问题,不断的根据
MimetypesFile 添加,现在 mustang 做了相应的类 FileTypeMap。
下面是个例子,
package test;

import javax.activation.*;
import java.io.*;

public class FileTypes {
 public static void main(String args[]) {
  FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
  String path;
  if (args.length == 0) {
   path = "d:/";
  } else {
   path = args[0];
  }
  File dir = new File(path);
  File files[] = dir.listFiles();
  for (int i = 0; i < files.length; i++) {
   File file = files[i];
   System.out
     .println(file.getName() + ": " + map.getContentType(file));
  }
 }
}
可以显示出文件的类型,下载还是打开,遗憾的是 java 5 提供的 for each mustang 却
不支持了,for (File file: files) 将是个错误。
java 实现用本地程序打开文件,就是实现 ShellExecute 的功能是个困难的事情,在
mustang 这个变得 simple。

import java.awt.*;
import java.io.*;

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 = "d:/";