Java Collection How to - Load a properties file in the startup directory








Question

We would like to know how to load a properties file in the startup directory.

Answer

  /*w  ww  .j a v  a2  s.c o  m*/


import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class Main {

  public static void main(String args[]) throws Exception {
    Properties props = new Properties();

    props = new java.util.Properties();
    String path = new Main().getClass().getProtectionDomain().getCodeSource().getLocation()
        .toString().substring(6);
    FileInputStream fis = new FileInputStream(new File(path + "\\myprops.props"));
    props.load(fis);
    System.out.println(props);

  }
}