2010-08-15 106 views
7

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

我该如何实施这种情况?

+3

“记事本”是什么意思?在Windows上使用一个糟糕的文本编辑程序,或TextArea控件?原谅我承担“事情”,但听起来你不知道Swing/AWT的基础知识。 – aviraldg 2010-08-15 11:24:51

+1

...你想打开记事本程序,或者你在记事本中创建的文本文件吗? – Stephen 2010-08-15 11:25:07

回答

6

假设您希望启动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这是要执行的工作目录的第三个参数 - 因此,您可以启动相对于程序工作目录存储的文本文件。

2

使用SWT,你可以启动任何 如果你想模拟双击windows中的文本,这是不可能的,只有一个普通的JRE。您可以使用本机库一样SWT和使用下面的代码打开一个文件:

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

如果你不希望使用第三方lib中,你应该知道,你知道在哪里的notepad.exe是(或者它在路径中可见):

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

阿帕奇common-exec是处理外部进程执行一个好的图书馆。

更新:更完整的回答你的问题,可以发现here

20

尝试

if (Desktop.isDesktopSupported()) { 
    Desktop.getDesktop().edit(file); 
} else { 
    // dunno, up to you to handle this 
} 

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

+0

您可能想要添加有关所需Java版本的详细信息以便使其工作。 – 2010-08-15 12:39:16

+1

嘿,不知道。凉。但是 - 第二行必须阅读'Desktop.getDesktop()。edit(file);'。并且该文件必须被创建,否则它会抱怨'IllegalArgumentException'。 – 2010-08-15 12:42:51

+0

谢谢@Andreas,修复它。确保文件存在是guilgamos的工作的一部分:)无论如何,+1还是不错的 – whiskeysierra 2010-08-15 15:35:16

2

在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(); 
} 
10

(假设你想记事本打开“myfile.txt的” :)

ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt"); 
pb.start(); 
2
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(); 
     } 
0

如果你开始用命令的命令行记事本你可以做到这一点,最好的:记事本开始

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

保存数组的字符串命令,并给它像参数在exec

Process runtimeProcess = Runtime.getRuntime().exec(startNotepadAdmin2); 
runtimeProcess.waitFor(); 
相关问题