Java Properties load from file in class path

Description

Java Properties load from file in class path


import java.net.URL;
import java.util.Properties;

public class Main {
  public static void main(String args[]) throws Exception {
    Properties props = new Properties();
    URL url = ClassLoader.getSystemResource("props.properties");
    props.load(url.openStream());//from   ww w  .  j ava  2  s.  co  m
    System.out.println("prop1 :\n " + props.get("prop1"));
    System.out.println("prop2 :\n " + props.get("prop2"));
  }
}

load a Properties file stored in a JAR

import java.net.URL;
import java.util.Properties;

import javax.swing.JApplet;

public class Main extends JApplet {
  public static void main(String[] a) throws Exception {
    Properties p = new Properties();
    URL url = ClassLoader.getSystemResource("/com/java2s/config/system.props");
    if (url != null)
      p.load(url.openStream());/*from  w ww.j  a  va2s.c  o m*/

  }
}



PreviousNext

Related