2016-06-11 82 views
1

我正在寫入我的Java程序中的.txt文件,一旦完成鍵入,請按Enter鍵。然後我想打開你正在寫的文件,這裏是我如何去做這件事。如何在Java中打開文本文件

PrintWriter writer ; 
    try { 
     writer = new PrintWriter(file.getPath(), "UTF-8"); 
     writer.println(decodedMessage); 
     writer.close(); 
     try { 
      pr = runtime.exec(file.getAbsolutePath()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } catch (FileNotFoundException | UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

所以要清除一些變量時,該decodedMessage是存儲的東西你打字,然後將其寫入到文件那路徑是預定義的字符串。 runtime是RunTime對象,pr是Process對象。我想要將剛寫入的文件打開。但是,當我運行此代碼我碰到下面的錯誤

java.io.IOException: Cannot run program "c:\users\owner\this.txt": CreateProcess error=193, %1 is not a valid Win32 application 
at java.lang.ProcessBuilder.start(Unknown Source) 
at java.lang.Runtime.exec(Unknown Source) 
at java.lang.Runtime.exec(Unknown Source) 
at java.lang.Runtime.exec(Unknown Source) 
at com.encdec.commandline.CommandLineRead.decodeFile(CommandLineRead.java:108) 
at com.encdec.commandline.CommandLineRead.executeEncodingDirectory(CommandLineRead.java:44) 
at com.encdec.listeners.ButtonCommand.actionPerformed(ButtonCommand.java:40) 
at javax.swing.JTextField.fireActionPerformed(Unknown Source) 
at javax.swing.JTextField.postActionEvent(Unknown Source) 
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source) 
at javax.swing.SwingUtilities.notifyAction(Unknown Source) 
at javax.swing.JComponent.processKeyBinding(Unknown Source) 
at javax.swing.JComponent.processKeyBindings(Unknown Source) 
at javax.swing.JComponent.processKeyEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$500(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 
    Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application 
at java.lang.ProcessImpl.create(Native Method) 
at java.lang.ProcessImpl.<init>(Unknown Source) 
at java.lang.ProcessImpl.start(Unknown Source) 
... 46 more 

這是非常漫長和恐嚇,我只是似乎無法與同樣的問題在網上找到合適的人。任何幫助非常感謝!通過這條線

pr = runtime.exec(file.getAbsolutePath()); 

+1

您嘗試執行你的文件,但它是可執行的嗎? –

+0

我想這不是因爲它是一個文本文件,所以你想怎麼處理你的文本文件? –

回答

2

能否請您更換以下行

pr = runtime.exec("notepad "+file.getAbsolutePath()); 

附加告訴我結果?

從檔案的絕對路徑c:\users\owner\this.txt看來,您好像是在Windows平臺上。但如果你想以平臺獨立的方式打開文件,下面的行可能會幫助你:

if(!GraphicsEnvironment.isHeadless()){ 
    java.awt.Desktop.getDesktop().open(file); 
}else{ 
    System.out.println("No display is available."); 
} 
+0

剛剛完成我的解決方案,就是這樣。 – Chewy

+0

非常感謝 – Luke

1

我不知道你試圖調用一個文本文件;

我可以建議你閱讀與BufferedReader例如,輸出的文本文件JTextArea或者,如果你不想使用GUI輸出到控制檯作爲官方的例子顯示:

import java.io.BufferedReader; 
import java.io.FileReader; 

public class Main { 
    public static void main(String[] argv) throws Exception { 

    BufferedReader in = new BufferedReader(new FileReader(file.getAbsolutePath())); 
    String str; 
    while ((str = in.readLine()) != null) { 
     System.out.println(str);//outputs to console 
    } 
    in.close(); 
    } 
} 

附:無論如何記住使用exec意味着你肯定知道應用程序(你想打開文本文件)是真的安裝;