如何在java中指定getResourceAsStream()方法的路径

svmlkihl  于 2023-02-28  发布在  Java
关注(0)|答案(4)|浏览(173)

我知道这个问题已经问了好几次了,但我还是不能用那些解决方案来解决它。
我有一个maven项目,还有一个Config.java文件,位于consumer/src/main/java,内容如下:

import java.util.Properties;

public class Config {
    Properties configFile;
    public Config() {
        configFile = new Properties();
        try {
            configFile.load(this.getClass().getClassLoader().
                    getResourceAsStream("property_table.config.txt"));
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public String getProperty(String key) {
        String value = this.configFile.getProperty(key);
        return value;
    }
    public static void main(String[] args) {
        Config config = new Config();
        System.out.println("URL: " + config.getProperty("URL"));
        System.out.println("PASSWORD: " + config.getProperty("PASSWORD"));
    }
}

我一直得到nullpointer exception。我知道这是因为它找不到文件property_table.config.txt
一开始我把property_table_config.txt文件和www.example.com文件放在同一个文件夹(consumer/src/main/java/)中Config.java,然后尝试使用/property_table_config.txt和“property_table_config.txt”,但它们都不起作用。
然后我试着用绝对路径,不起作用。试着用/main/java/property_table_config,也不起作用。
然后我看到了这个解决方案:https://stackoverflow.com/a/2103625/8159477。所以我创建了一个名为resources的目录,并将其放在主文件夹下(即文件夹的路径为consumer/src/main/resources),然后在resources下创建一个子文件夹config。在将property_table_config.txt文件放在那里之后,我将代码更改为:

configFile.load(this.getClass().getClassLoader().getResourceAsStream("/config/property_table.config.txt"));

但还是不行。谁能给予点提示吗?任何建议都将不胜感激!!

xqkwcwgp

xqkwcwgp1#

According to Class.getResourceAsStream :
This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

  • If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
  • Otherwise, the absolute name is of the following form:

modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
This is how I understand the comments:

  • If you use ClassLoader.getResourceAsStream , send the absolute path from package root, but omitting the first / .
  • If you use Class.getResourceAsStream , send either a path relative the the current Class object (and the method will take the package into account), or send the absolute path from package root, starting with a / .

But in addition to this, you need to be cognizant of your build system. With maven, resource files are stored under src/main/resources .
So, in your case, I believe making the following changes should resolve the issue:

  • Put the file in src/main/resources .
  • Change the code to
this.getClass()
      .getResourceAsStream("/property_table.config.txt")
      //or `Config.class.getResource...
  • Alternatively, use
this.getClass().getClassLoader()
                  .getResourceAsStream("property_table.config.txt")`

I've tried this with a similar setup, and it works as expected.

jdg4fx2g

jdg4fx2g2#

ClassLoader().getResourceAsStream()只在类路径中查找文件。您需要将配置文件放在类路径的目录中。
所以,你有选择:

  • 当你运行你的java应用程序从命令行你可以设置路径到目录在-cp参数或CLASSPATH系统变量.点有:要从中获取配置文件的目录必须位于类路径中,而不是文件。(例如,如果文件位置为c:\my_projects\test-project\config\my_config.properties,并且c:\my_projects\test-project\位于类路径中,则getResourceAsStream调用将为ClassLoader().getResourceAsStream("config/my_config.properties")
  • 你可以把你的文件打包成jar文件,jar文件的根目录是getResourceAsStream("config/my_config.properties")的起始点
  • 如果您的Maven项目是一个jar项目,则需要使用Maven资源插件将其他资源放入jar中。

顺便说一句:Maven不会将src/main/java/目录中的任何内容放入jar文件(如果您没有为资源插件显式指定它)
如果您将Eclipse之类的IDE与Maven项目一起使用,那么src/main/resources是构建类路径的一部分。请仔细检查它是否在那里,如果不在,请执行“更新Maven项目”或手动添加它。
不过,只有在IDE中运行项目时,ClassLoader才会从src/main/resources文件夹中看到属性文件,而不是从独立的Jar文件中(如果您没有打包文件或在类路径中提供位置)。

apeeds0o

apeeds0o3#

嗨你能试试这个吗

String dirBase = new ClassPathResource("property_table.config.txt").getURI().getPath().replace("property_table.config.txt", "");
cwxwcias

cwxwcias4#

你能试试下面的代码吗。
配置类.getResourceAsStream(“属性_表.配置. txt”)
更新:这是我试过的代码。

package test;

import java.util.Properties;

public class Config
{
  Properties configFile;

  public Config()
  {
    configFile = new Properties();
    try
    {
      configFile.load(Config.class.getResourceAsStream("property_table.config.txt"));
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public String getProperty(String key)
  {
    String value = this.configFile.getProperty(key);
    return value;
  }

  public static void main(String[] args)
  {
    Config config = new Config();
    System.out.println("URL: " + config.getProperty("URL"));
    System.out.println("PASSWORD: " + config.getProperty("PASSWORD"));
  }
}

我把property_table. config. txt文件放在测试包中,它工作正常。

相关问题