我正在执行词法分析器和解析器,但是我的应用程序找不到我的文件文本:(

问题描述 投票:-1回答:1
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();
    }

运行后出现以下内容,该文件无法建立和读取,希望您能为我提供帮助...

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)
java java-io filenotfoundexception
1个回答
0
投票
您没有说明将help.txt文件放置在项目中的位置,也没有说明项目的运行方式(命令行与NetBeans中的对比),因此无法精确诊断问题。但是仍然可以提出解决方案。

首先,请勿执行main()中的处理,因为您将无法调用获取有关应用程序的运行时详细信息所需的非静态方法getClass()

第二,为了使代码在NetBeans中运行项目时正常工作,可以将getClass()直接放在项目的根目录下,然后将其找到。但是,如果您随后尝试从命令行运行项目的jar文件,则会得到help.txt,因为jar文件中不会包含FileNotFoundException

因此,请直接在现有软件包的help.txt文件夹下,为resources文件创建一个help.txt文件夹。例如,如果您的项目在src下有一个名为redcompiler的包,则项目结构的一部分应如下所示:

src

这将确保src
    redcompiler
        RedCompiler.java
        resources
            help.txt
将包含在您的jar文件中。然后,在help.txt方法中,您可以编写在NetBeans中运行以及从命令行运行项目的jar文件时将成功访问main()的代码。但是,尽管有多种方法可以执行此操作,但使用help.txt并不是OP中所示的一种。有关更多详细信息,请参见对FileInputStream的接受的答案。

相反,您可以使用How can I access a txt file in a jar with FileInputStream?读取help.txt,您可以使用通过BufferedReaderInputStream获得的ClassLoader.getResourceAsStream()实例化它。这是使用两种方法读取Class.getResource()的示例代码:

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)

注意:

    我使用
  • File> New Project ...> Java with Ant> Java Application在NetBeans中创建了项目。
  • 在构建项目时,将通过命令行进行的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> 调用记录在
  • Output
  • 窗口中。这是项目的结构,如NetBeans的
  • Files
  • 面板中所示:java
© www.soinside.com 2019 - 2024. All rights reserved.