gpt4 book ai didi

java - 如何在 Java 中打开一个文本文件

转载 作者:行者123 更新时间:2023-11-29 08:44:03 26 4
gpt4 key购买 nike

我正在我的 java 程序中写入一个 .txt 文件,一旦您完成输入,您就可以按回车键。然后我想打开您正在写入的文件,这就是我尝试执行此操作的方式。

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

通过这一行:

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

关于java - 如何在 Java 中打开一个文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37764683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com