Jump to Real's How-to Main page

Load a properties file

public class LoadProps {

   public static void main(String args[]) throws Exception{
      h.doit();
      h.doitagain();
      }
   public void doit() throws Exception{
      // properties in the classpath
      java.util.Properties props = new java.util.Properties();
      java.net.URL url = ClassLoader.getSystemResource("myprops.props");
      props.load(url.openStream());
      System.out.println(props);
     }
   public void doitagain() throws Exception{
      // properties in the startup directory
      java.util.Properties props = new java.util.Properties();
      String path = getClass().getProtectionDomain().getCodeSource().
                    getLocation().toString().substring(6);
      java.io.FileInputStream fis = new java.io.FileInputStream
         (new java.io.File( path + "\\myprops.props"));
      props.load(fis);
      System.out.println(props);
     }
}

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]