2010-08-15 77 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();