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!
Sie versuchen, Ihre Datei auszuführen, aber ist es ausführbar? –
Ich denke, es ist nicht so, wie es eine Textdatei ist, was willst du mit deiner Textdatei genau machen? –