How to get parameters from Java Properties File

Published on May 13, 2016 by Daniel Lanza


Parameters are read from Java properties file and printed on console.

Java Properties File

The content of Java Properties File is the following:

name = Daniel
lastName = Lanza
age = 29

And the file is located in the desktop:

User Java class

package com.dalanz.file;

import java.util.Properties;

public class ReadProperties {

    private String fileSource;

    public ReadProperties (String fileSource) {
        this.fileSource = fileSource;

    public String getProperty(String key) {

        Properties prop = new Properties();
        InputStream input = null;

        try {
            input = new FileInputStream(this.fileSource);

            // Load a properties file

            // Get the property value
            return prop.getProperty(key);
        } catch (IOException ex) {
            return null;
        } finally {
            if (input != null) {
                try {
                } catch (IOException e) {

Main Class


import com.dalanz.file.*;

public class test {

    public static void main(String[] args) {

        String sourceFile = "/Users/dalanz/Desktop/";

        ReadProperties prop = new ReadProperties(sourceFile);

        System.out.println("Name: " + prop.getProperty("name"));
        System.out.println("Last name: " + prop.getProperty("lastName"));
        System.out.println("Age: " + prop.getProperty("age"));



Name: Daniel
Last Name: Lanza
Age: 29
comments powered by Disqus

See also

How to convert Java object to JSON and JSON to Java object with Gson

A simple Java object will become to JSON by using Gson. Besides, this will become into Java object. Maven dependencies <dependencies> <dependency> <groupId>> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> </dependencies> User Java class package com.dalanz.json; import java.util.ArrayList; import java.util.List; public class User { private String firstName; private String lastName; private int age; private List<String> messages; public User(){} public User(String firstName, String lastName, int age, List<String> messages) { this.firstName = firstName; this.lastName = lastName; this.

Read more

How to zip a folder and unzip a Zip file in Java

A folder with subfolders and files will be zipped. Besides, the zip file will be unzipped. Zip directory Java class package com.dalanz.file; import; import; import; import; import java.util.ArrayList; import; import; import; public class ZipDirectory { private String folderPath; private String zipPath; private static ArrayList<String> fileList = null; private static String sourceFolder = null; public String getFolderPath() { return folderPath; } public void setFolderPath(String folderPath) { this.

Read more

How to convert File into Array of Bytes, and Array of Bytes to File in Java

Array of bytes will be calculated from a file, and then, that array of bytes will be used to create a file with different filename. Array of bytes Java class package com.dalanz.file; import; import; import; public class ArrayBytesFile { // Attributes private File file; private byte[] bFile; // Constructors public ArrayBytesFile() { this.file = null; this.bFile = null; } public ArrayBytesFile (File file) { this.file = file; fileToBFile(); } public ArrayBytesFile (String filePath, byte[] bFile) { this.

Read more