【问题标题】:Load a property file in Java在 Java 中加载属性文件
【发布时间】:2012-01-02 17:08:57
【问题描述】:

每当我尝试通过以下方法加载属性文件时。我在getClass() as-上收到错误-

无法从 Object 类型对非静态方法 getClass() 进行静态引用

public static void main(String[] args) {

        ---
        ---

    loadProperties(line);

}

private static void loadProperties(String line) {
        Properties prop = new Properties();
        InputStream in = getClass().getResourceAsStream("foo.properties");
        try {
            prop.load(in);
            for(Object str: prop.keySet()) {
                Object value = prop.getProperty((String) str);
                System.out.println(str+" - "+value);
            }
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

任何建议我该如何克服这个问题。

【问题讨论】:

    标签: java properties


    【解决方案1】:

    您不能从静态方法调用getClass。您要么需要在非静态方法中执行此操作:

    class MyClass {
       public static void main(String[] args) {
          MyClass obj = new MyClass();
          obj.loadProperties(line);    
       }
    
       private void loadProperties(String line) {
            Properties prop = new Properties();
            InputStream in = getClass().getResourceAsStream("foo.properties");
            ...  
       }
    }
    

    或使用在静态上下文中工作的类文字,例如

    InputStream in = MyClass.class.getResourceAsStream("foo.properties");
    

    【讨论】:

    • 请注意,如果您使用 getClass() 而不是类字面量,则在存在子类时可能会发生有趣的事情(您可能会得到与您想象的不同的包)。
    【解决方案2】:

    从静态方法usign访问项目类路径中的属性文件 getClass 试试看:

    import java.io.InputStream;
    import java.util.Properties;
    import java.io.IOException;
    public class HelperClass {
         public static String getProperty() {
            Properties properties = new Properties();
            String wadlURI = "";
               try {
                   InputStream inputStream = HelperClass.class.getClassLoader().getResourceAsStream("configuration.properties");
                    properties.load(inputStream);
                   wadlURI = properties.getProperty("wadlurl");
               } catch (IOException e) {
                   e.printStackTrace();
                 }
              return wadlURI;
          }
    }
    

    【讨论】:

      【解决方案3】:

      我在 IntelliJ IDEA 中遇到了同样的问题,并且采取了相同的步骤。但我没有将资源文件夹标记为“资源根”。完成后问题为我解决了。

      【讨论】:

        【解决方案4】:

        要访问属性文件并将其加载到应用程序,您可以使用以下内容。

        public static void loadPropertiesToMemory() {
            Properties prop = new Properties();
            InputStream input = null;
            String filePathPropFile = "configuration.properties";
            try {
                input = App.class.getClassLoader().getResourceAsStream(filePathPropFile);
                prop.load(input);
            } catch (IOException ex) {
                ex.printStackTrace();
            } 
        }
        

        App 是该方法所在的类。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-27
          • 2011-06-14
          • 1970-01-01
          • 2010-09-24
          • 2014-12-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多