首页 > 解决方案 > IntelliJ Idea - 通过 IntelliJ 运行时找不到输入文件(bank.in)

问题描述

我有一个作业,我是在 Linux、Visual Studio Code 和命令行上完成的。在我需要调试我的代码之前,它工作得非常好。所以我迁移到了 Windows 10,因为我在那里安装了 IntelliJ IDEA。我编译了代码并将输入文件“bank.in”放在与编译的“MyClass.class”相同的文件夹中

但是,当我从 IntelliJ 运行程序时,我的代码捕获了一个异常,即当它与“MyClass.class”位于同一文件夹中时,它无法找到文件“bank.in”。

我创建 bank.in 的方法是,右键单击 IntelliJ 中的 out 文件夹并添加一个新文件并从那里添加 bank.in 内容

我试过通过 cmd.exe 运行它java MyClass,它运行良好。没有捕获到异常。

但是当通过 IntelliJ IDEA 运行时,它显示

    Cannot find bank.in...

    Exiting the program...

这是我输入文件的代码的一部分。

public void main(String[] args)
{
    String fileName = "bank.in";

    FileReader bank = null;
    BufferedReader bankBuffered = null;

    try
    {
        bank = new FileReader(fileName);
        bankBuffered = new BufferedReader(bank);
    }
    catch(FileNotFoundException f)
    {
        System.out.printf("%s is not found.%n%n", fileName);
        System.out.printf("Exiting the program...%n");
        System.exit(0);
    }
}

这是我的项目文件夹结构

MyProject
-.idea
    -encodings.xml
    -misc.xml
    -modules.xml
    -workspace.xml
-out
    -MyClass.class
    -bank.in
-src
    -MyClass.java

当我通过 cmd.exe 运行它时,它工作正常。有什么解决方法吗?谢谢你。

标签: javaintellij-ideawindows-10bufferedreaderfilereader

解决方案


推荐阅读