在Java中从src文件夹打开文本文件时出错
我正在制作一个 Java 程序,想要打开一个 text(notepad)
文件,该文件已保存在我的 Java 程序的 src
文件夹中,我尝试通过下面的代码,但它给出了错误。 “Runtime
类型中的方法 exec(String)
不适用于参数 (InputStream)
,有什么建议吗?谢谢
Runtime runtime = Runtime.getRuntime();
InputStream lic = this.getClass().getClassLoader().getResourceAsStream("MyFile.txt");
Process process = runtime.exec(lic);
I am making a Java program and want to open a text(notepad)
file ,that i have saved in src
folder in my Java Program, I have tried to do it by the following code , but it is giving error. "The method exec(String)
in the type Runtime
is not applicable for the arguments(InputStream)
, Any suggestions ? Thanks
Runtime runtime = Runtime.getRuntime();
InputStream lic = this.getClass().getClassLoader().getResourceAsStream("MyFile.txt");
Process process = runtime.exec(lic);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法执行输入流,它只是字节的集合,而不是文件。
您应该将此文件存储在项目内的其他位置(例如资源文件夹)并使用该路径打开该文件。它看起来是这样的:
You can not execute an input-stream, it's just a collection of bytes, not a file.
You should store this file somewhere else inside your project (like a resources folder) and use the path to open the file. Here's how it would look like:
所有
Runtime.exec
方法均不接受InputStream
http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html
从
InputStream 读取文件
,使用BufferedReader
None of the
Runtime.exec
methods accept anInputStream
http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html
To read a file from an
InputStream
, use aBufferedReader
我认为这就是您想要做的:
请参阅
ProcessBuilder
Javadocs。I think this is what you're trying to do:
See
ProcessBuilder
Javadocs.