package com.tij.io.file;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
/**
* java打开文件
* @author guoyoujun
* @date 2014-3-17
*/
public class JavaOpenFile {
/**
* java.awt.Desktop 用来打开文件的类; 首先能否打开文件要满足以下几点
* <p>1、操作平台是否支持桌面方式
* 2、文件是否存在
* <p>如果文件不存在则会抛出异常java.lang.IllegalArgumentException
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File("/Users/GYJ/java1.txt");
if (!Desktop.isDesktopSupported()) {
System.out.println("sorry! Desktop is not supported");
return ;
} else {
Desktop desktop = Desktop.getDesktop();
if (file.exists()) {
desktop.open(file);
}
file = new File("/Users/GYJ/a.pdf");
if (file.exists()) {
desktop.open(file);
}
}
}
}
===================================================================
1、如果打开的文件在系统没有对应的应用程序或者说文件不存在会报异常!!
java.io.IOException
2、如果打开的是PDF文件会使用Adobe Acrobat Reader打开
4779

被折叠的 条评论
为什么被折叠?



