Intellij - 无法从同一模块中的其他包读取文件

Intellij - Not able to read a file from other package in the same module

我正在使用 Intellij Idea - 2021.3.2 和 Java 8. 在 Intellij 中,我无法读取同一模块中另一个包中的文件。该文件位于主文件夹下,而不是资源文件夹下。但在 Eclipse 中,我能够阅读。

例如。下面的代码在 eclipse 和 intellij 中打印不同的输出。

    import java.net.URL;

public class TestMain {
    public static void main(String[] args) {
        URL resource = TestMain.class.getResource("/org/files/input.txt");
        if(resource == null) {
            System.out.println("Resource is null");
        } else {
            System.out.println("Resource found!!");
        }
    }
}

在 Intellij 中,代码打印“Resource is null”,但在 Eclipse 中,代码打印“Resource found!!”。

我需要在 intellij 中 enable/disable 进行任何设置吗?为什么代码在 Intellij Idea 中表现不同?

将文件移动到 resources/org/files。

.txt 文件不会根据 Maven 约定从源根目录复制到类路径。

如果要从源根目录复制这些文件,需要调整pom.xml配置:.

Eclipse 不遵循约定,您将在命令行 Maven 和 IDE 中得到不同的结果,这很糟糕。