I want to open Notepad program in MS Windows by Java code to open my text file.
Pls help me to do that.
解决方案
You can use the java.awt.Desktop if using Java 1.6, .txt is registered to the notepad and Desktop is supported:
if (!Desktop.isDesktopSupported()) {
System.err.println("Desktop not supported");
// use alternative (Runtime.exec)
return;
}
Desktop desktop = Desktop.getDesktop();
if (!desktop.isSupported(Desktop.Action.EDIT)) {
System.err.println("EDIT not supported");
// use alternative (Runtime.exec)
return;
}
try {
desktop.edit(new File("test.txt"));
} catch (IOException ex) {
ex.printStackTrace();
}
this way you can open/edit files in a more OS independent way.