Reading a properties file in Java

I have the following code trying to read a properties file:

Properties prop = new Properties(); ClassLoader loader = Thread.currentThread().getContextClassLoader(); InputStream stream = loader.getResourceAsStream("myProp.properties"); prop.load(stream); 

I get an exception on the last line. In particular:

 Exception in thread "main" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:418) at java.util.Properties.load0(Properties.java:337) at java.util.Properties.load(Properties.java:325) at Assignment1.BaseStation.readPropertyFile(BaseStation.java:46) at Assignment1.BaseStation.main(BaseStation.java:87) 

thanks Nikos

+96
java properties
Nov 27 '11 at 12:37
source share
15 answers

Based on your exception, InputStream is null, which means the class loader cannot find your properties file. I assume myProp.properties is at the root of your project, if in this case you need the previous slash:

 InputStream stream = loader.getResourceAsStream("/myProp.properties"); 
+79
Nov 27 '11 at 12:44
source share


You can find the information on this page:
http://www.mkyong.com/java/java-properties-file-examples/

 Properties prop = new Properties(); try { //load a properties file from class path, inside static method prop.load(App.class.getClassLoader().getResourceAsStream("config.properties")); //get the property value and print it out System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } 
+51
Nov 29 '12 at 18:38
source share

You can use the ResourceBundle class to read the properties file.

 ResourceBundle rb = ResourceBundle.getBundle("myProp.properties"); 
+23
Nov 27 '11 at 13:05
source share
 Properties prop = new Properties(); try { prop.load(new FileInputStream("conf/filename.properties")); } catch (IOException e) { e.printStackTrace(); } 

conf/filename.properties base on the project root directory

+9
Jan 24 '17 at 6:49
source share

You cannot use this keyword as -

 props.load(this.getClass().getResourceAsStream("myProps.properties")); 

in a static context.

It would be best to get the application context, for example -

 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/app-context.xml"); 

then you can load the resource file from the class path -

 //load a properties file from class path, inside static method prop.load(context.getClassLoader().getResourceAsStream("config.properties")); 

This will work for both static and non-static contexts, and the best part of this properties file can be in any package / folder included in the application class path.

+7
Dec 02
source share

Your file should be accessible as com/example/foo/myProps.properties in the classpath. Then download it as:

 props.load(this.getClass().getResourceAsStream("myProps.properties")); 
+5
Nov 27 '11 at 13:03
source share

Verify that the file name is correct and that the file is indeed in the class path. getResourceAsStream() returns null if it is not, which causes the last line to throw an exception.

If myProp.properties is in the root directory of your project, use /myProp.properties instead.

+3
Nov 27 '11 at 12:45
source share

To use the context loader.getResourceAsStream("myPackage/myProp.properties") .

The leading '/' does not work with the ClassLoader.getResourceAsStream(String) method.

Alternatively, you can use the Class.getResourceAsStream(String) method, which uses '/' to determine if the path is absolute or relative to the location of the class.

<strong> Examples:

 myClass.class.getResourceAsStream("myProp.properties") myClass.class.getResourceAsStream("/myPackage/myProp.properties") 
+3
Nov 27 '11 at 12:55
source share

You can use java.io.InputStream to read the file as shown below:

 InputStream inputStream = getClass().getClassLoader().getResourceAsStream(myProps.properties); 
+3
Dec 24 '14 at 17:33
source share

For a read properties file with the original order:

  File file = new File("../config/edc.properties"); PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config); layout.load(new InputStreamReader(new FileInputStream(file))); for(Object propKey : layout.getKeys()){ PropertiesConfiguration propval = layout.getConfiguration(); String value = propval.getProperty((String) propKey).toString(); out.print("Current Key:" + propkey + "Current Value:" + propval + "<br>"); } 
+1
Feb 15 '19 at 11:02
source share

None of the current answers show a close of InputStream (this will result in a file descriptor leak) and / or has nothing to .getResourceAsStream() with .getResourceAsStream() returning null when the resource is not found (this will result in a NullPointerException with the confused message "inStream parameter is null" ) . You need something like the following:

 String propertiesFilename = "server.properties"; Properties prop = new Properties(); try (var inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFilename)) { if (inputStream == null) { throw new FileNotFoundException(propertiesFilename); } prop.load(inputStream); } catch (IOException e) { throw new RuntimeException( "Could not read " + propertiesFilename + " resource file: " + e); } 
+1
Mar 06 '19 at 2:01
source share

If your path to the properties file and your path to the java class are the same, then you should do this.

For example:

SRC / MyPackage / MyClass.java

SRC / MyPackage / MyFile.properties

 Properties prop = new Properties(); InputStream stream = MyClass.class.getResourceAsStream("MyFile.properties"); prop.load(stream); 
0
Dec 01 '18 at 10:46
source share

Many of the answers here describe dangerous methods in which they create a file input stream, but do not receive a link to the input stream to close the stream later. This causes input streams to freeze and memory leaks. The correct way to load properties should be similar to the following:

  Properties prop = new Properties(); try(InputStream fis = new FileInputStream("myProp.properties")) { prop.load(fis); } catch(Exception e) { System.out.println("Unable to find the specified properties file"); e.printStackTrace(); return; } 

Note the creation of the file input stream in the try-with-resources block. Since FileInputStream is autocomplete, it will be automatically closed after exiting the try-with-resources block. If you want to use a simple try block, you must explicitly close it using fis.close(); in the block, finally .

0
Feb 19 '19 at 16:21
source share

if your config.properties is not in the src / main / resource directory and is in the project root directory, then you need to do something like below: -

 Properties prop = new Properties(); File configFile = new File(myProp.properties); InputStream stream = new FileInputStream(configFile); prop.load(stream); 
0
Mar 14 '19 at 6:41
source share

Specify the path starting with src, as shown below:

 src/main/resources/myprop.proper 
-2
Mar 06 '19 at 12:10
source share



All Articles