Reputation: 13
I'm trying to use a config file that holds a list of hosts/websites and a time frequency for each one.
ex.
google.com 15s
yahoo.com 10s
My objective is to ping each website from the config file at every time period (15 secs).
Should I just read the config file and input the hosts/time into separate arrays?
Seems like there is a more efficient method...
Upvotes: 0
Views: 129
Reputation: 19607
Here is a quick rundown of how to use a properties file.
You can create a file with the extension .properties
(if under Windows make sure you have file extensions displayed) in the root of your project. The properties can be defined as pairs:
google.com=15
yahoo.com=10
In Java,
To get the ping time of a particular URL:
final String path = "config.properties";
Properties prop = new Properties();
int pingTimeGoogle = prop.load(new FileInputStream(path)).getProperty("google.com");
To cycle through the properties and get the whole list:
final String path = "config.properties";
Properties props = new Properties().load(new FileInputStream(path));
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(key + "=" + props.getProperty(key));
}
Edit: And here's a handy way to transform properties into a Map
(Properties implements the Map
interface):
final String path = "config.properties";
Properties props = new Properties().load(new FileInputStream(path));
Map<String, Integer> pingUrlTimes = new HashMap<String, Integer>((Map) props);
Cycling through the HashMap
can be done like this:
Iterator iterator = pingUrlTimes.keySet().iterator(); // Get Iterator
while (iterator.hasNext()) {
String key = (String) iterator.next();
System.out.println(key + "=" + pingUrlTimes.get(key) );
}
Upvotes: 0
Reputation: 308753
Why use two arrays when the two items are so intimately related?
I'd put them into a Map:
Map<String, Integer> pingUrlTimes = new HashMap<String, Integer>();
pingUrlTimes.put("google.com", 15);
pingUrlTimes.put("yahoo.com", 10);
int pingTime = pingUrlTimes.get("google.com");
Upvotes: 2