如何用java打开记事本文件?
我想在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试
确保该文件存在。感谢 Andreas_D 指出了这一点。
Try
Make sure the file exists. Thanks to Andreas_D who pointed this out.
(假设您希望记事本打开“myfile.txt”:)
(assuming you want notepad to open "myfile.txt" :)
假设您希望启动 Windows 程序
notepad.exe
,您正在寻找exec
函数。您可能想要调用类似的内容:例如,在我的计算机上,记事本位于
C:\Windows\notepad.exe
:这将打开记事本,并打开文件 test.txt 进行编辑。
请注意,您还可以为
exec
指定第三个参数,它是要执行的工作目录 - 因此,您可以启动一个相对于程序工作目录存储的文本文件。Assuming you wish to launch the windows program
notepad.exe
, you are looking for theexec
function. You probably want to call something like:For example, on my machine notepad is located at
C:\Windows\notepad.exe
: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.使用 SWT,您可以启动任何
如果您想在 Windows 中模拟双击文本,则仅使用普通 JRE 是不可能的。您可以使用 SWT 等本机库并使用以下代码打开文件:
如果您不想使用第三方库,您应该知道并且知道 notepad.exe 在哪里(或者它在 PATH 中可见) :
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:
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):
Apache common-exec is a good library for handling external process execution.
UPDATE: A more complete answer to your question can be found here
在IDE(Eclipse)中,它抱怨“C:\path\to\notepad.exe C:\path\to\file.txt”。
所以我使用了以下对我有用的方法,让我和我的 IDE 开心:o)
希望这能帮助其他人。
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.
如果您在命令行中使用以下命令启动记事本,则可以做到这一点: start notepad
保存字符串命令数组并像 exec 中的参数一样给出它
You could do this the best if you start notepad in command line with command: start notepad
Save array of string commands and give it like parametr in exec