Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 1.15 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I utilize a config file with my Java program?
  2. google.com  15s
  3. yahoo.com   10s
  4.        
  5. google.com=15
  6. yahoo.com=10
  7.        
  8. final String path = "config.properties";
  9.  
  10. Properties prop = new Properties();
  11.  
  12. int pingTimeGoogle = prop.load(new FileInputStream(path)).getProperty("google.com");
  13.        
  14. final String path = "config.properties";
  15.  
  16. Properties props = new Properties().load(new FileInputStream(path));
  17. Enumeration e = props.propertyNames();
  18.  
  19. while (e.hasMoreElements()) {
  20.     String key = (String) e.nextElement();
  21.     System.out.println(key + "=" + props.getProperty(key));
  22. }
  23.        
  24. final String path = "config.properties";
  25.  
  26. Properties props = new Properties().load(new FileInputStream(path));
  27.  
  28. Map<String, Integer> pingUrlTimes = new HashMap<String, Integer>((Map) props);
  29.        
  30. Iterator iterator = pingUrlTimes.keySet().iterator(); // Get Iterator
  31.  
  32. while (iterator.hasNext()) {
  33.     String key = (String) iterator.next();
  34.  
  35.     System.out.println(key + "=" +  pingUrlTimes.get(key) );
  36. }
  37.        
  38. Map<String, Integer> pingUrlTimes = new HashMap<String, Integer>();
  39. pingUrlTimes.put("google.com", 15);
  40. pingUrlTimes.put("yahoo.com", 10);
  41.  
  42. int pingTime = pingUrlTimes.get("google.com");