java - 无法读取 IntelliJ 中的属性文件

标签 java eclipse intellij-idea

我的文件夹结构是:

Main
  |
  .idea
  resources 
  src
     |
      main
        |
        java
          |
          com.read.properties
                |
                config.properties 
                a.java

我在a.java中的代码是:

Properties prop = new Properties();
    InputStream input = null;

    try {

        String filename = "config.properties";
        input = getClass().getClassLoader().getResourceAsStream(filename);
        if (input == null) {
            System.out.println("Sorry, unable to find " + filename);
            return Response.status(400).build();
        }

        prop.load(input);

        Enumeration<?> e = prop.propertyNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = prop.getProperty(key);
            System.out.println("Key : " + key + ", Value : " + value);
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

我无法读取属性文件,我不断收到 IO 错误。我尝试更改默认类路径

来自运行->编辑配置

当我在 eclipse 中尝试类似的操作时,我可以很容易地读取该文件,这让我相信我在 intelliJ 中的某些配置是错误的。如果有人能指出我正确的方向,我将非常感激

最佳答案

我对此不确定,但在我的 IDEA (Maven) 项目中,我将资源保存在 src/main|test/resources 下,我可以很好地阅读它们:

src
   main
       resources
           path
               file


getClass().getClassLoader().getResourceAsStream("path/file");

您需要指定文件的“完整”路径。

关于java - 无法读取 IntelliJ 中的属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24023931/

相关文章:

java - 如何在正则表达式中匹配 "any character"?

java - 如何使用 SlideMenu 检测 ListView 行单击

java - 如何在 Eclipse 中添加 Java 项目而不是 Jar

eclipse - 在 Eclipse 中创建项目后更改 Maven Archetype?

tomcat - 在 intellij 中配置 'Run/Debug'

java - 关系 "table name"不存在 postgresql

java - 有没有一种方法可以在一个heroku应用程序中部署两个容器?

java - 如何将自己的jar导入到android项目中(ADT 20)

java - intelliJ 找不到特定的方法

java - 如何判断在 IntelliJ 中调试时是否触发了 Java 异常?