简体   繁体   中英

Path of properties file in java

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

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.

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")

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

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

In a static method I use

ClassLoader.getSystemResourceAsStream("name.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")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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