如何在java中打开记事本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3487149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open the notepad file in java?
提问by guilgamos
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.
我想在我的 Java 程序中打开记事本。假设我有一个按钮,如果我点击这个按钮,记事本就会出现。我已经有了一个文件名和一个目录。
How can I implement this case?
我该如何实施这个案例?
回答by Stephen
Assuming you wish to launch the windows program notepad.exe
, you are looking for the exec
function. You probably want to call something like:
假设您希望启动 windows 程序notepad.exe
,您正在寻找该exec
功能。你可能想调用类似的东西:
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
:
例如,在我的机器上记事本位于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.
这将打开记事本,并打开文件 test.txt 进行编辑。
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.
请注意,您还可以指定exec
要从中执行的工作目录的第三个参数- 因此,您可以启动一个相对于程序工作目录存储的文本文件。
回答by Mohsen
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:
使用SWT,您可以启动任何 如果您想在 Windows 中模拟双击文本,仅使用普通的 JRE 是不可能的。您可以使用像 SWT 这样的本机库并使用以下代码打开文件:
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):
如果你不想使用第三方库,你应该知道并且你知道 notepad.exe 在哪里(或者它在 PATH 中可见):
runtime.exec("notepad.exe c:\path\to\file.txt");
Apache common-execis a good library for handling external process execution.
Apache common-exec是一个很好的处理外部进程执行的库。
UPDATE: A more complete answer to your question can be found here
更新:可以在此处找到更完整的问题答案
回答by whiskeysierra
Try
尝试
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().edit(file);
} else {
// dunno, up to you to handle this
}
Make sure the file exists. Thanks to Andreas_D who pointed this out.
确保文件存在。感谢 Andreas_D 指出这一点。
回答by mark
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.
在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();
}
回答by Hal
(assuming you want notepad to open "myfile.txt" :)
(假设您希望记事本打开“myfile.txt”:)
ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();
回答by Riyasam
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();
}
回答by Seda
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
保存字符串命令数组并在 exec 中给它像参数一样
Process runtimeProcess = Runtime.getRuntime().exec(startNotepadAdmin2);
runtimeProcess.waitFor();