如何通过Java代码执行记事本

Cha*_*Pye 1 java

我想通过Java代码在MS Windows中打开记事本程序来打开我的文本文件.

请帮助我这样做.

use*_*421 10

您可以使用java.awt.Desktopif使用Java 1.6,.txt注册到记事本并支持Desktop:

    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();
    }
Run Code Online (Sandbox Code Playgroud)

这样,您就可以以更独立于操作系统的方式打开/编辑文件.