Load Properties from Properties file - Java File Path IO

Java examples for File Path IO:Property Files

Description

Load Properties from Properties file

Demo Code

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.Properties;

public class Main {

  public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    try (InputStream in = Files.newInputStream(FileSystems.getDefault()
        .getPath(//from w w w .  j  a  v a  2s  . c  o m
            System.getProperty("user.dir") + File.separator
                + "db_props.properties"));) {
      props.load(in);
      in.close();
    } catch (IOException ex) {
      ex.printStackTrace();

    }
    /*
        hostname = props.getProperty("host_name");
        port = props.getProperty("port_number");
        database = props.getProperty("db_name");
        username = props.getProperty("username");
        password = props.getProperty("password");
        driver = props.getProperty("driver");
        jndi = props.getProperty("jndi");    
    
    */
    
  }
}

Result


Related Tutorials