gui*_*mos 7 java notepad runtime.exec
我想在我的Java程序中打开记事本.假设我有一个按钮,如果单击此按钮,将出现记事本.我已经有了文件名和目录.
我该如何实施这个案子?
whi*_*rra 21
尝试
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().edit(file);
} else {
// dunno, up to you to handle this
}
Run Code Online (Sandbox Code Playgroud)
确保文件存在.感谢Andreas_D指出了这一点.
Hal*_*Hal 10
(假设你想要记事本打开"myfile.txt":)
ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();
Run Code Online (Sandbox Code Playgroud)
假设您要启动Windows程序notepad.exe,您正在寻找该exec功能.你可能想要调用类似的东西:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\path\\to\\notepad.exe C:\\path\\to\\file.txt");
Run Code Online (Sandbox Code Playgroud)
例如,在我的机器上,记事本位于C:\Windows\notepad.exe:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Windows\\notepad.exe C:\\test.txt");
Run Code Online (Sandbox Code Playgroud)
这将打开记事本,文件test.txt打开进行编辑.
请注意,您还可以指定exec要从中执行的工作目录的第三个参数- 因此,您可以启动相对于程序工作目录存储的文本文件.
| 归档时间: |
|
| 查看次数: |
57589 次 |
| 最近记录: |