2016-06-11 17 views
1

Ich schreibe in eine .txt-Datei in meinem Java-Programm und sobald Sie mit der Eingabe fertig sind, drücken Sie Enter. Dann möchte ich die Datei öffnen, in der du geschrieben hast, hier versuche ich dies zu tun.Wie öffne ich eine Textdatei in 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(); 
    } 

So einige Variablen zu klären, die decodedMessage ist die Zeichenfolge, die das Zeug speichert die Eingabe von Ihnen, es schreibt sie dann in einer Datei das ist Pfad vorgegeben ist. runtime ist ein RunTime-Objekt und pr ist ein Process Objekt. Ich möchte, dass die Datei, die gerade geschrieben wurde, geöffnet wird. Aber wenn ich diesen Code ausführen bekomme ich folgende Fehler

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 

Es ist sehr lang und einschüchternd und ich kann einfach nicht, dass jemand scheinen online mit dem gleichen Problem zu finden. Jede Hilfe wird sehr geschätzt!

+1

Sie versuchen, Ihre Datei auszuführen, aber ist es ausführbar? –

+0

Ich denke, es ist nicht so, wie es eine Textdatei ist, was willst du mit deiner Textdatei genau machen? –

Antwort

2

Könnten Sie bitte folgende Zeile:

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

von dieser Linie:

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

add mir das Ergebnis sagen?

Aus dem absoluten Pfad c:\users\owner\this.txt der Datei sieht es so aus, dass Sie auf Windows-Plattform sind. Aber im Fall, dass Sie die Datei in plattformunabhängige Art und Weise zu öffnen, können folgende Zeilen helfen Ihnen:

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

Habe gerade meine Lösung fertig und das wars. – Chewy

+0

Das hat perfekt funktioniert, vielen Dank – Luke

1

Ich bin nicht sicher, was Sie eine Textdatei zu exec versuchen Aufruf;

Ich kann empfehlen, die Datei mit BufferedReader zum Beispiel und gibt den Text zu JTextArea lesen oder wenn Sie wollen es als offizielles Beispiel auf der Konsole nicht GUI-Ausgang verwenden, zeigt:

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

P. S. Wie auch immer erinnern Sie sich mit exec bedeutet, dass Sie sicher wissen, die Anwendung (mit der Sie Textdatei öffnen möchten) ist wirklich installiert;