简体   繁体   English

在哪里放置属性文件?

[英]Where to put a properties file?

I tried to read a .properties file and have code as following: 我尝试读取.properties文件并具有以下代码:

public final class Config  {
  static {
    Properties properties = new Properties();
    InputStream propertiesStream = Object.class.getResourceAsStream("config.properties");

    if (propertiesStream != null) {
      try {
        properties.load(propertiesStream);
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      System.out.println("file not found");
    }
  }
}

but it keeps saying file not found. 但它一直说文件没找到。

The content of properties is 属性的内容是

pwd=passw0rd

Anyone knows how to solve this problem? 谁知道如何解决这个问题?

它应该在classpath中,把它放到你的根源包中,如果它的maven项目把它放到src/main/resources目录中

it should be in WebContent/Web-Inf/ folder 它应该在WebContent/Web-Inf/文件夹中

and in you xml file define the bean like this : 并在你的xml文件中定义这样的bean:

<bean id="propertyConfigurer" class="com.your.project.util.properties.ApplicationProperties">
    <property name="locations">
        <list>
            <value>/WEB-INF/application.properties</value>
        </list>
    </property>
</bean>

You could also keep config.properties in the same folder as Config.java. 您还可以将config.properties保存在与Config.java相同的文件夹中。

//InputStream propertiesStream = Object.class.getResourceAsStream("config.properties");
InputStream propertiesStream   = Config.class.getResourceAsStream("config.properties");

You can have two choices of selecting the path, 您可以有两种选择路径的选择,

  1. Open the file containing folder and get the path and save that path in the string with file like, 打开包含文件夹的文件并获取路径并将该路径保存在包含文件的字符串中,

    InputStream propertiesStream = Object.class.getResourceAsStream(path + File.seperator + "config.properties"); InputStream propertiesStream = Object.class.getResourceAsStream(path + File.seperator +“config.properties”);

  2. Save the file in src path, 将文件保存在src路径中,

    WorkSpace -> Project Name -> Copyhere WorkSpace - >项目名称 - > Copyhere

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

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