Java I';我正在做一个lexer和parser,但我的应用程序可以';找不到我的文件文本:(
运行上述代码后,我得到以下错误。Java I';我正在做一个lexer和parser,但我的应用程序可以';找不到我的文件文本:(,java,java-io,filenotfoundexception,Java,Java Io,Filenotfoundexception,运行上述代码后,我得到以下错误。 注意:英文错误消息为: 无法找到并读取文件 我希望你能帮助我 public class RedCompiler { /** * @param args the command line arguments * @throws java.io.IOException */ public static void main(String[] args) throws IOException { Syste
注意:英文错误消息为: 无法找到并读取文件 我希望你能帮助我
public class RedCompiler {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("help.txt"));
Lexer lexer = new Lexer();
Parser parser = new Parser(lexer);
parser.start();
}
线程“main”java.io.FileNotFoundException:help.txt中的异常
位于java.io.FileInputStream.open0(本机方法)
在java.io.FileInputStream.open(FileInputStream.java:195)
位于java.io.FileInputStream。(FileInputStream.java:138)
位于java.io.FileInputStream。(FileInputStream.java:93)
位于redcompiler.redcompiler.main(redcompiler.java:26)
C:\Users\itzel\AppData\Local\NetBeans\Cache\8.2\executor snippets\run.xml:53:Java返回:1
生成失败(总时间:0秒)
您没有说明将help.txt文件放置在项目中的什么位置,也没有说明项目是如何运行的(命令行与NetBeans中),因此无法精确诊断问题。但仍然可以提出解决方案
首先,不要在main()
中执行处理,因为您将无法调用获取应用程序运行时详细信息所需的非静态方法
其次,为了在NetBeans中运行项目时简单地让代码正常工作,您可以将help.txt
直接放在项目根目录下,它就会被找到。但是如果您尝试从命令行运行项目的jar文件,您会得到一个FileNotFoundException
,因为help.txt
会ot已包含在jar文件中
因此,请直接在src
文件夹下的现有包下为help.txt
文件创建一个resources
文件夹。例如,如果项目在src
下有一个名为redcompiler
的包,则项目结构的一部分如下所示:
Exception in thread "main" java.io.FileNotFoundException: help.txt (El sistema no puede encontrar el archivo especificado)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at redcompiler.RedCompiler.main(RedCompiler.java:26)
C:\Users\itzel\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
这将确保help.txt
将包含在jar文件中
方法,当您在NetBeans中运行时,以及从命令行运行项目的jar文件时,您可以编写成功访问help.txt
的代码。但是,尽管有多种方法可以实现这一点,但使用文件输入流
并不是其中之一。请参阅f的公认答案或者更多细节
相反,您可以使用BufferedReader
读取help.txt
,您可以使用ClassLoader.getResourceAsStream()
或Class.getResource()
获取的InputStream
对其进行实例化
src
redcompiler
RedCompiler.java
resources
help.txt
这是在NetBeans中运行项目时的输出:
package redcompiler;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
public class RedCompiler {
public static void main(String[] args) throws IOException, URISyntaxException {
new RedCompiler().demo();
}
void demo() throws IOException, URISyntaxException {
// Read file using ClassLoader.getResourceAsStream()
String path = "redcompiler/resources/help.txt";
InputStream in = getClass().getClassLoader().getResourceAsStream(path);
System.out.println("Using ClassLoader.getResourceAsStream(): path=" + path + ", InputStream=" + in);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
System.out.println("Using ClassLoader.getResourceAsStream(): " + reader.readLine() + '\n');
// Read file using Class.getResource()
path = "resources/help.txt";
URL url = getClass().getResource(path);
System.out.println("Using Class.getResource(): path=" + path + ", URL=" + url);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("Using Class.getResource(): " + reader.readLine());
}
}
这是从命令行运行项目的jar文件时的输出:
run:
Using ClassLoader.getResourceAsStream(): path=redcompiler/resources/help.txt, InputStream=java.io.BufferedInputStream@15db9742
Using ClassLoader.getResourceAsStream(): This is the content of file help.txt.
Using Class.getResource(): path=resources/help.txt, URL=file:/D:/NB112/RedCompiler/build/classes/redcompiler/resources/help.txt
Using Class.getResource(): This is the content of file help.txt.
BUILD SUCCESSFUL (total time: 0 seconds)
注:
- 我在NetBeans中使用File>newproject…>Java with Ant>Java应用程序创建了这个项目
- 构建项目时,从命令行进行的
调用将记录在输出窗口中java
- 这是项目的结构,如NetBeans中的文件面板所示:
help.txt
。工作目录是由System.getProperty(“user.dir”)返回的值
。文件help.txt
似乎不在该目录中。因此,要么移动该文件,要么在代码中添加文件路径。@skomisa您不能两全其美。问题是打开一个文件,而不是一个资源。没有涉及资源的解决方案是有效的。@Naria文件“help.txt”在哪里实际定位?@user207421 OP显示语句System.setIn(new FileInputStream(“help.txt”)
的运行时错误(FileNotFoundException
),并明确声明“无法找到和读取该文件”。他们的问题只是使用main()中的FileInputStream读取文本文件help.txt
。这绝对与“无论他的语言是什么,都是源代码”无关。我认为你完全误解了这个问题。你在第三段中提出了这一主张,但你没有提到编译问题中明确说明的源文件的编译器。事实上,你的第三段毫无意义。new FileInputStream()
不会查看JAR文件内部。您将无法调用非静态方法getClass()
方法getClass()
不是获取相关Class
实例的唯一方法。
Microsoft Windows [Version 10.0.18363.476]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\Users\johndoe>C:\Java\jdk1.8.0_221/bin/java -jar "D:\NB112\RedCompiler\dist\RedCompiler.jar"
Using ClassLoader.getResourceAsStream(): path=redcompiler/resources/help.txt, InputStream=sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@55f96302
Using ClassLoader.getResourceAsStream(): This is the content of file help.txt.
Using Class.getResource(): path=resources/help.txt, URL=jar:file:/D:/NB112/RedCompiler/dist/RedCompiler.jar!/redcompiler/resources/help.txt
Using Class.getResource(): This is the content of file help.txt.
C:\Users\johndoe>