java中属性文件的路径

发布于 2024-11-19 23:33:29 字数 977 浏览 2 评论 0原文

我有一个位于默认包内的属性文件,并且我在其中使用属性文件的类也在同一个默认包中。如果我只使用文件名而不使用任何路径,则会出现错误。显然这是不正确的,因为我应该提供某种路径来引用 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);
    }

I have a property file which is inside a default package and the class in which I am using the properties file is also in the same default package. If I use just the file name without any path I am getting error. Obviously that is not correct as I should give some kind of path to refer to tat file. I will build the application make it as a jar file so how should I give the path as the properties file should go inside that jar file. I am using Netbeans IDE.

EDIT

 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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

隐诗 2024-11-26 23:33:29

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

但请注意,不鼓励使用默认包(上面的方法适用于任何包)。

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

Use getClass().getResourceAsStream("foo.properties")

But note that using the default package is discouraged (the above will work with any package).

Your code doesn't work because the FileInputStream(..) uses paths relative to the current user directory (see java.io.File documentation). So it looks for foo.properties in /home/usr/ or c:\documents and settings\usr. Since your .properties file is on the classpath you can read it as such - throug the Class.getResourceAsStream(..) method.

離殇 2024-11-26 23:33:29

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

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

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

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

As others have indicated, you should load it from the classpath instead of as a file if you want to be able to load it from a jar. You want the Class.getResourceAsStream() method or the ClassLoader.getResourceAsStream() method. However, using getClass().getResourceAsStream("pos_config.properties") is dangerous because you're using a path that's resolved relative to the given class, and a subclass could change the location against which it's resolved. Therefore, it's safest to name an absolute path within the jar:

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

or even better, use a class literal instead of getClass():

Foo.class.getResourceAsStream("pos_config.properties")
眼角的笑意。 2024-11-26 23:33:29

您是否尝试从当前工作目录获取属性文件,或者尝试将其作为资源获取?听起来你应该使用。

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);
}

Are you trying to get the properties file from the current working directory or are you trying to get it as a resource? It sounds like you should be using.

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

to load a file from the current directory

properties.load(new FileInputStream(filename));

My guess is what you really want is this.

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);
}
明天过后 2024-11-26 23:33:29

无论如何,我建议将所有属性文件放在资源文件夹中。

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

右键单击->新->源文件夹

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

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

因此,对于您的特定问题,您可以像这样访问它:

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

I would anyway recommend putting all the property files in a resource folder.

In Eclipse you can create a source folder with:

Right-click -> new -> Source Folder

I bet there is something similar in Netbeans. Put all your property file in there. Later you can access them with the following code:

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

So for your specific problem you would access it like that:

pro.load(new FileInputStream(YourClass.class.getResource("/pos_config.properties")));
探春 2024-11-26 23:33:29

在我使用的静态方法中

ClassLoader.getSystemResourceAsStream("name.properties");

In a static method I use

ClassLoader.getSystemResourceAsStream("name.properties");

酒解孤独 2024-11-26 23:33:29

这不会编译:

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

正确使用:

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

This will not compile:

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

Proper use:

pro.load(YourClass.class.getResourceAsStream("/pos_config.properties")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文