要使用默认的已注册或关联的应用程序编辑文件,我们可以调用该java.awt.Desktop.edit(File)方法。在下面的代码段中,我们将编辑一个PNG文件。使用该类的edit()方法Desktop将打开PNG文件的默认注册应用程序。例如,在Windows上,可以是Linux操作系统上的Windows Paint或GIMP。
package org.nhooo.example.awt;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class RunningDefaultAppEdit {
public static void main(String[] args) {
File file = new File("logo.png");
try {
//使用默认程序编辑文件类型的文件。在这个例子中
// 我们将启动默认的注册程序来编辑PNG图像。
Desktop desktop = Desktop.getDesktop();
desktop.edit(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}