In this article we are going to discuss about how to read data from properties and store in to map.
## Web Site information site1 = www.google.com site2 = www.becbe.com site3 = www.linux.com site4 = www.motorola.com site5 = www.apple.com
Filename: ReadPropertiesFileToMap.java
package com.becbe.sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/**
*
*/
/**
* @author tponnam
*
*/
public class ReadPropertiesFileToMap {
private String filePath="d:\\sample.properties";
/**
* This method validate the file existence
* @return
*/
private File getPropertiesFile()
{
File file = new File(filePath);
if( !file .exists()){
try {
file.createNewFile();
} catch (IOException e) {
}
}
return file;
}
/**
* This Method reads data from properties file and store into Map
* @return
*/
public HashMap getSBX5kNodeToProxyPortMap(){
HashMap map = new HashMap();
Properties properties = new Properties();
try {
properties.load(new FileInputStream(getPropertiesFile()));
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
map.put(key, value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
public static void main(String[] args) {
ReadPropertiesFileToMap fileToMap = new ReadPropertiesFileToMap();
HashMap map = fileToMap.getSBX5kNodeToProxyPortMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry mEntry = (Map.Entry) iter.next();
System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
}
}
}
Output:
site5 : www.apple.com site4 : www.motorola.com site3 : www.linux.com site2 : www.becbe.com site1 : www.google.com
0 comments :
Post a Comment