How to get parameters from Java Properties File

Published on May 13, 2016 by Daniel Lanza

 java

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.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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
            prop.load(input);

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

Main Class

package test.com.dalanz;

import com.dalanz.file.*;

public class test {

    public static void main(String[] args) {

        String sourceFile = "/Users/dalanz/Desktop/test.properties";

        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"));

    }
}

Output

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>com.google.code.gson/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 java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; 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 java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class ArrayBytesFile { // Attributes private File file; private byte[] bFile; // Constructors public ArrayBytesFile() { this.file = null; this.bFile = null; } public ArrayBytesFile (File file) { this.

Read more