繁体   English   中英

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

[英]I'm doing a lexer and parser but my application can't find my file text :(

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)

您没有说明将help.txt文件放在项目中的什么位置,也没有说明项目是如何运行的(命令行与 NetBeans 中的),因此无法准确诊断您的问题。 但是仍然可以提出解决方案。

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

其次,为了在 NetBeans 中运行项目时简单地使代码正常工作,您可以将help.txt直接放在项目的根目录下,这样就可以找到它。 但是,如果您随后尝试从命令行运行项目的 jar 文件,您将收到FileNotFoundException因为help.txt不会包含在 jar 文件中。

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

src
    redcompiler
        RedCompiler.java
        resources
            help.txt

这可确保help.txt将包含在您的 jar 文件中。 然后,在main()方法中,您可以编写代码,当您在 NetBeans 中运行以及从命令行运行项目的 jar 文件时,这些代码将成功访问help.txt 但是,虽然有多种方法可以做到这一点,但使用FileInputStream ,如 OP 所示,并不是其中之一。 请参阅如何使用 FileInputStream 访问 jar 中的 txt 文件? 更多细节。

相反,您可以使用BufferedReader读取help.txt ,您可以使用ClassLoader.getResourceAsStream()Class.getResource()获得的InputStream进行实例化。 以下是使用两种方法读取help.txt示例代码:

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

}

这是在 NetBeans 中运行项目时的输出:

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)

这是从命令行运行项目的 jar 文件时的输出:

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>

笔记:

  • 我使用File > New Project... > Java with Ant > Java Application在 NetBeans 中创建了项目。
  • 当您构建项目时,将从命令行进行的java调用记录在“输出”窗口中。
  • 这是项目的结构,如 NetBeans 中的“文件”面板所示: 项目结构

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM