Java Property files

Load Properties From File

props = new Properties();
String myFile = "myFile";
props.load(new BufferedInputStream(new FileInputStream(myFile)));
  final static String propFile = "my.properties";
 
  private static void loadPropertiesFromFile() 
          throws RuntimeException {
      prop = new Properties();
      try {
          prop.load(new BufferedInputStream(
          new FileInputStream(propFile)));
      } catch (FileNotFoundException ex) {
          logger.log(Level.SEVERE, null, ex);
          throw new RuntimeException(ex);
      } catch (IOException ex) {
          logger.log(Level.SEVERE, null, ex);
          throw new RuntimeException(ex);
      }
  }

Load Properties From Classpath (e.g. jar file)

private Properties loadProperties() {
  InputStream is = this.getClass().getClassLoader()
    .getResourceAsStream("my.properties");
  Properties prop = new Properties();
  if (is != null){
    try{
      prop.load(is);
    }catch(IOException ioe){
      log.error(ioe);
    }
  }else{
    log.error("my.properties not found!");
    System.exit(1);
  }
  return prop;
}
  final static String propFile = "my.properties";
 
  public static void loadPropertiesFromClaspath()
          throws RuntimeException {
      prop = new Properties();
      InputStream is = KrbProperties.class.getResourceAsStream(
              propFile);
      if (is == null){
          throw new Exception(
                  "Property file not found: " 
                  + propFile);
      }
 
      try {
          prop.load(is);
      } catch (IOException ex) {
          logger.log(Level.SEVERE, null, ex);
          throw new Exception(ex);
      }
  }

Struts MessageResources

MessagResources
MessagResources  rs = (MessageResources)getServlet()
 .getServletContext().getAttribute(Action.MessageKey+appPrefix);
String text = rs.getMessage("key");

System Properties

* Access system properties

System.getProperty("path.separator");
System.getProperties();

* System properties:
file.separator: Character that separates components of a file path. This is / on UNIX and \ on Windows.
java.class.path: Path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property.
java.home: Installation directory for Java Runtime Environment (JRE)
java.vendor: JRE vendor name
java.vendor.url: JRE vendor URL
java.version: JRE version number
line.separator: Sequence used by operating system to separate lines in text files
os.arch: Operating system architecture
os.name: Operating system name
os.version: Operating system version
path.separator: Path separator character used in java.class.path
user.dir: User working directory
user.home: User home directory
user.name: User account name

Replace System Properties for Debugging

  public static void replaceSystemProperties(){
      logger.warning("Replacing system properties implementation...");
      Properties p = System.getProperties();
      if (p != null && p instanceof SysProperties){
          logger.info("Already using " + SysProperties.class.getName());
      }
 
      SysProperties sp = new SysProperties(System.getProperties());
      System.setProperties(sp);
          logger.info("Replaced system properties with " 
                  + SysProperties.class.getName());
  }
public class SysProperties extends Properties {
    static final Logger logger = Logger.getLogger(
            SysProperties.class.getName());
 
    public SysProperties(Properties defaults) {
        super(defaults);
    }
 
    @Override
    public synchronized Object setProperty(String key, String value) {
        logger.log(Level.WARNING, 
                "Setting system property {0} with value {1}", 
                new String[]{key, value});
        return super.setProperty(key, value);
    }   
}

References
Smartly load your properties
Got resources?

This entry was posted in java and tagged , , . Bookmark the permalink.