如何用Java打开文本文件

时间:2016-06-11 14:08:46

标签: java

我正在写一个java程序中的.txt文件,一旦你完成输入,你就按Enter键。然后我想打开你写的文件,这就是我试图这样做的方式。

    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

这是非常漫长和令人生畏的,我似乎无法在网上找到任何有相同问题的人。非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

请您更换以下内容:

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

这一行:

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.");
}

答案 1 :(得分: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();
  }
}

P.S。无论如何记得使用exec意味着您确实知道应用程序(您要打开文本文件)确实已安装;

答案 2 :(得分:1)

如果您希望它在系统文本编辑器中打开(或任何程序配置为打开具有该文件扩展名的文件),请执行以下操作:

java.awt.Desktop.getDesktop().open(file);

https://docs.oracle.com/javase/8/docs/api/java/awt/Desktop.html#open-java.io.File-