Java Utility Get Integer Value From Properties File

Load the property File

Properties prop = new Properties();
   String propFileName = "/opt/my.properties";
   try(InputStream inputStream = new FileInputStream(propFileName)) {
	prop.load(inputStream);
    }

Now get the Integer Value from the loaded property File

	 * 
	 * @param prop
	 * @param propertyNameInFile
	 * @param defaultValue
	 * @return the value from the file or the default value
	 */
	private static int getIntegerValueFromFile(Properties prop, String propertyNameInFile, int defaultValue) {

		String propertyValueInFile = prop.getProperty(propertyNameInFile);
		int value = defaultValue;
		if (StringUtils.isNotBlank(propertyValueInFile)) {
			try {
				value = Integer.parseInt(propertyValueInFile);
			} catch (Exception e) {
				logger.error(e, e);
			}
		}
		return value;
	}

Like this post? Don’t forget to share it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.