如何用java打开记事本文件?

发布于 2024-09-14 13:33:22 字数 87 浏览 9 评论 0原文

我想在我的 Java 程序中打开记事本。假设我有一个按钮,如果我单击此按钮,记事本就会出现。 我已经有了文件名和目录。

我该如何实施这个案例?

I want to open Notepad in my Java program. Suppose that I have one button if I click this button the notepad will appear.
I already have a file name and a directory.

How can I implement this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧伤慢歌 2024-09-21 13:33:22

尝试

if (Desktop.isDesktopSupported()) {
    Desktop.getDesktop().edit(file);
} else {
    // I don't know, up to you to handle this
}

确保该文件存在。感谢 Andreas_D 指出了这一点。

Try

if (Desktop.isDesktopSupported()) {
    Desktop.getDesktop().edit(file);
} else {
    // I don't know, up to you to handle this
}

Make sure the file exists. Thanks to Andreas_D who pointed this out.

最单纯的乌龟 2024-09-21 13:33:22

(假设您希望记事本打开“myfile.txt”:)

ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();

(assuming you want notepad to open "myfile.txt" :)

ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();
北方的韩爷 2024-09-21 13:33:22

假设您希望启动 Windows 程序 notepad.exe,您正在寻找 exec 函数。您可能想要调用类似的内容:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\path\\to\\notepad.exe C:\\path\\to\\file.txt");

例如,在我的计算机上,记事本位于 C:\Windows\notepad.exe

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Windows\\notepad.exe C:\\test.txt");

这将打开记事本,并打开文件 test.txt 进行编辑。

请注意,您还可以为 exec 指定第三个参数,它是要执行的工作目录 - 因此,您可以启动一个相对于程序工作目录存储的文本文件。

Assuming you wish to launch the windows program notepad.exe, you are looking for the exec function. You probably want to call something like:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\path\\to\\notepad.exe C:\\path\\to\\file.txt");

For example, on my machine notepad is located at C:\Windows\notepad.exe:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Windows\\notepad.exe C:\\test.txt");

This will open notepad with the file test.txt open for editing.

Note you can also specify a third parameter to exec which is the working directory to execute from - therefore, you could launch a text file that is stored relative to the working directory of your program.

玻璃人 2024-09-21 13:33:22

使用 SWT,您可以启动任何
如果您想在 Windows 中模拟双击文本,则仅使用普通 JRE 是不可能的。您可以使用 SWT 等本机库并使用以下代码打开文件:

    org.eclipse.swt.program.Program.launch("c:\path\to\file.txt")

如果您不想使用第三方库,您应该知道并且知道 notepad.exe 在哪里(或者它在 PATH 中可见) :

    runtime.exec("notepad.exe c:\path\to\file.txt");

Apache common-exec 是一个很好的处理外部进程执行的库。

更新:您的问题的更完整答案可以在此处

Using SWT, you can launch any
If you want to emulate double-clicking on a text in windows, it's not possible only with a plain JRE. You can use a native library like SWT and use the following code to open a file:

    org.eclipse.swt.program.Program.launch("c:\path\to\file.txt")

If you don't want to use a third-party lib, you should know and you know where notepad.exe is (or it's visible in PATH):

    runtime.exec("notepad.exe c:\path\to\file.txt");

Apache common-exec is a good library for handling external process execution.

UPDATE: A more complete answer to your question can be found here

雨落□心尘 2024-09-21 13:33:22

在IDE(Eclipse)中,它抱怨“C:\path\to\notepad.exe C:\path\to\file.txt”。
所以我使用了以下对我有用的方法,让我和我的 IDE 开心:o)
希望这能帮助其他人。

String fpath;
fPath =System.getProperty("java.io.tmpdir")+"filename1" +getDateTime()+".txt";
//SA - Below launches the generated file, via explorer then delete the file "fPath"
       try { 
        Runtime runtime = Runtime.getRuntime();         
        Process process = runtime.exec("explorer " + fPath);

Thread.sleep(500); //lets give the OS some time to open the file before deleting

    boolean success = (new File(fPath)).delete();
    if (!success) {
        System.out.println("failed to delete file :"+fPath);
        // Deletion failed
    }

} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace(); 
}

In IDE (Eclipse) it compains about "C:\path\to\notepad.exe C:\path\to\file.txt" .
So i have used the following which works for me keeping me and my IDE happy :o)
Hopefully this will help others out there.

String fpath;
fPath =System.getProperty("java.io.tmpdir")+"filename1" +getDateTime()+".txt";
//SA - Below launches the generated file, via explorer then delete the file "fPath"
       try { 
        Runtime runtime = Runtime.getRuntime();         
        Process process = runtime.exec("explorer " + fPath);

Thread.sleep(500); //lets give the OS some time to open the file before deleting

    boolean success = (new File(fPath)).delete();
    if (!success) {
        System.out.println("failed to delete file :"+fPath);
        // Deletion failed
    }

} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace(); 
}
夏了南城 2024-09-21 13:33:22
String fileName = "C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\Student Project\\src\\studentproject\\resources\\RealWorld.chm";
        String[] commands = {"cmd", "/c", fileName};
        try {
            Runtime.getRuntime().exec(commands);
//Runtime.getRuntime().exec("C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\SwingTest\\src\\Test\\RealWorld.chm");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
String fileName = "C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\Student Project\\src\\studentproject\\resources\\RealWorld.chm";
        String[] commands = {"cmd", "/c", fileName};
        try {
            Runtime.getRuntime().exec(commands);
//Runtime.getRuntime().exec("C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\SwingTest\\src\\Test\\RealWorld.chm");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
寄风 2024-09-21 13:33:22

如果您在命令行中使用以下命令启动记事本,则可以做到这一点: start notepad

String[] startNotePadWithoutAdminPermissions = new String[] {"CMD.EXE", "/C", "start" "notepad" };

保存字符串命令数组并像 exec 中的参数一样给出它

Process runtimeProcess = Runtime.getRuntime().exec(startNotepadAdmin2);
runtimeProcess.waitFor();

You could do this the best if you start notepad in command line with command: start notepad

String[] startNotePadWithoutAdminPermissions = new String[] {"CMD.EXE", "/C", "start" "notepad" };

Save array of string commands and give it like parametr in exec

Process runtimeProcess = Runtime.getRuntime().exec(startNotepadAdmin2);
runtimeProcess.waitFor();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文