Property file path in java

I have a properties file that is inside the default package, and the class in which I use the properties file is also in the same package by default. If I use only the file name without any path, I get an error. Obviously, this is not true, since I have to give some way to access the tat file. I will create the application by making it as a jar file, and how should I specify the path, since the properties file must go into this jar file. I am using the 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); } 
+6
source share
6 answers

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

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

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

+9
source

As others have pointed out, you must load it from a class, not a file, if you want to load it from a jar. Do you want the Class.getResourceAsStream () or ClassLoader.getResourceAsStream () method . However, using getClass().getResourceAsStream("pos_config.properties") dangerous because you are using a path that is allowed relative to this class, and the subclass can change the location at which it was allowed. Thus, it is safer to name the absolute path in the bank:

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

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

 Foo.class.getResourceAsStream("pos_config.properties") 
+6
source

Are you trying to get the properties file from the current working directory or trying to get it as a resource? Sounds like you should use.

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

to download a file from the current directory

 properties.load(new FileInputStream(filename)); 

My guess is what you really need.

 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); } 
+4
source

In any case, I recommend placing all property files in the resource folder.

In Eclipse, you can create the source folder using

Right Click -> New -> Source Folder

I am sure there is something similar in Netbeans. Put your entire properties file. Later you can access them with the following code:

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

So, for your specific problem, you will get access to it as follows:

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

In the static method, I use

ClassLoader.getSystemResourceAsStream ("name.properties");

+2
source

This will not compile:

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

Proper use:

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

Source: https://habr.com/ru/post/892767/


All Articles