Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Properties load

farenda 2015-09-11 0

Problem:

How to load Properties in Java? Properties class has methods to load properties from different sources – InputStream, Reader, and XML file. Here’s an example of Properties.load(InputStream).

Solution:

java.util.Properties has three methods to load Properties from files:

  • void load(InputStream inStream)
    Reads property list (key=value pairs) from given InputStream
  • void load(Reader reader)
    Reads property list (key=value paris) from given Reader
  • void loadFromXML(InputStream in)
    Loads all properties from XML document in format as produced by Properties.storeToXML() method.

In the following example we show how to use Properties.load(InputStream) to load a app-config.properties:

package com.farenda.java.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public class PropertiesLoad {

    public static void main(String[] args) throws IOException {
        Properties config = loadFromFile("app-config.properties");

        printToSystemOutput(config);
    }

    private static void printToSystemOutput(Properties config) throws IOException {
        config.store(System.out, "Loaded properties:");
    }

    private static Properties loadFromFile(String filename) throws IOException {
        Path configLocation = Paths.get(filename);
        System.out.println("Config location: " + configLocation.toString());
        try (InputStream stream = Files.newInputStream(configLocation)) {
            Properties config = new Properties();
            config.load(stream);
            return config;
        }
    }
}

Put the following content in app-config.properties and place it in your projects directory (or src/main/resources if you are using Maven or Gradle):

database=localhost:2017
username=user
password=secretpassword

Running the code produces the following output:

Config location: app-config.properties
#Loaded properties:
#Sun Sep 06 17:53:59 CEST 2015
password=secretpassword
database=localhost\:2017
username=user

The code for Properties.load(Reader) is the same. You just need to create a Reader instead of InputStream.

Share with the World!
Categories Java Tags java, java-basics
Previous: Java Properties store as XML
Next: Java Properties load from XML

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok