如何通过Java代码执行记事本 [英] How can I excute Notepad by Java code

查看:123
本文介绍了如何通过Java代码执行记事本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to open Notepad program in MS Windows by Java code to open my text file.

请帮助我这样做。

推荐答案

如果使用Java 1.6,可以使用 java.awt.Desktop > .txt 已注册到记事本并支持桌面:

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.

这篇关于如何通过Java代码执行记事本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆