【问题标题】:Path of properties file in javajava中属性文件的路径
【发布时间】:2011-10-05 12:16:21
【问题描述】:

我有一个位于默认包内的属性文件,并且我使用该属性文件的类也在同一个默认包中。如果我只使用没有任何路径的文件名,我会收到错误消息。显然这是不正确的,因为我应该给出某种路径来引用 tat 文件。我将构建应用程序使其成为一个 jar 文件,所以我应该如何给出路径,因为属性文件应该进入该 jar 文件。我正在使用 Netbeans IDE。

编辑

 Properties pro = new Properties();

    try {            
        pro.load(new FileInputStream("pos_config.properties"));
        pro.setProperty("pos_id", "2");
        pro.setProperty("shop_type", "2");
        pro.store(new FileOutputStream("pos_config.properties"), null);
        String pos_id = pro.getProperty("pos_id");
        if(pos_id.equals("")){
           pos_id="0" ;
        }
        global_variables.station_id = Integer.parseInt(pos_id);
        String shop_type = pro.getProperty("shop_type");
        if(shop_type.equals("")){
           shop_type="0" ;
        }
        global_variables.shop_type = Integer.parseInt(shop_type);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

【问题讨论】:

  • 请发布您用于访问属性文件的代码。
  • @little bunny foo foo:检查我的编辑..
  • 我不使用netbeans,但如果你在netbeans中点击右键文件,你不会有类似build path > include
  • 代码审查评论:看起来您的代码和该问题答案中的大多数代码示例都无法关闭输入流。这可能会给您带来麻烦。

标签: java resources properties jar


【解决方案1】:

使用getClass().getResourceAsStream("foo.properties")

但请注意,不鼓励使用默认包(以上内容适用于任何包)。

您的代码不起作用,因为FileInputStream(..) 使用相对于当前用户目录的路径(请参阅java.io.File 文档)。所以它在/home/usr/c:\documents and settings\usr 中寻找foo.properties。由于您的 .properties 文件位于类路径中,因此您可以这样读取它 - 通过 Class.getResourceAsStream(..) 方法。

【讨论】:

    【解决方案2】:

    您是想从当前工作目录中获取属性文件,还是想将其作为资源获取?听起来你应该使用。

    InputStream is = getClass().getResourceAsStream(filename);
    properties.load(is);
    

    从当前目录加载文件

    properties.load(new FileInputStream(filename));
    

    我猜你真正想要的是这个。

    try {            
        Properties pro = new Properties();
        pro.load(new FileInputStream("pos_config.properties"));
        String pos_id = pro.getProperty("pos_id");
        try {
            global_variables.station_id = Integer.parseInt(pos_id);
        } catch(Exception e) {
            global_variables.station_id = 0;
        }
        String shop_type = pro.getProperty("shop_type");
        try {
            global_variables.shop_type = Integer.parseInt(shop_type);
        } catch(Exception e) {
            global_variables.shop_type = 0;
        }
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    

    【讨论】:

    • 是的,我正在尝试从当前目录中获取它。我正在进入 public static void main() 那么我如何在静态上下文中使用这个非静态方法?
    • 当您使用properties 引用时,您可以使用属性的非静态方法
    • getResourceAsInputStream() -> getResourceAsStream()
    • 您的代码 sn-p 只使用工作目录,而不是类路径。问题指出属性文件将被捆绑到一个jar中,因此需要从类路径中加载它。
    • 当我将文件从默认包移动到新包并使用 getClass().class.getResource("/properties/pos_config.properties") 之类的东西时,我解决了这个问题。
    【解决方案3】:

    我还是建议将所有属性文件放在资源文件夹中。

    在 Eclipse 中,您可以使用以下命令创建源文件夹:

    右键->新建->源文件夹

    我敢打赌 Netbeans 中也有类似的东西。把你所有的财产文件放在那里。稍后您可以使用以下代码访问它们:

    AnyClass.class.getResource("/image.png")
    

    因此,对于您的具体问题,您可以这样访问它:

    pro.load(new FileInputStream(YourClass.class.getResource("/pos_config.properties")));
    

    【讨论】:

      【解决方案4】:

      正如其他人所指出的,如果您希望能够从 jar 加载它,则应该从类路径加载它而不是作为文件加载。您需要Class.getResourceAsStream() 方法或ClassLoader.getResourceAsStream() 方法。但是,使用getClass().getResourceAsStream("pos_config.properties") 是危险的,因为您使用的是相对于给定类解析的路径,并且子类可能会更改解析它的位置。因此,在 jar 中命名绝对路径是最安全的:

      getClass().getResourceAsStream("/pos_config.properties")
      

      或者更好的是,使用类文字而不是 getClass():

      Foo.class.getResourceAsStream("pos_config.properties")
      

      【讨论】:

        【解决方案5】:

        这不会编译:

        pro.load(new FileInputStream(YourClass.class.getResource("/pos_config.properties")));
        

        正确使用:

        pro.load(YourClass.class.getResourceAsStream("/pos_config.properties")));
        

        【讨论】:

          【解决方案6】:

          在我使用的静态方法中

          ClassLoader.getSystemResourceAsStream("name.properties");

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-03-11
            • 2010-12-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-23
            • 1970-01-01
            相关资源
            最近更新 更多