I want to write and read 'HashMap' to file.
My 'HashMap' is:
Map<String, ArrayList<Descipline>> mapDis = new HashMap<String, ArrayList<Descipline>>();
and I write to file like this:
String root = Environment.getExternalStorageDirectory().toString();
Properties properties = new Properties();
for (Map.Entry<String, ArrayList<Descipline>> entry : mapDis.entrySet()){
properties.put(entry.getKey(), entry.getValue());
}
properties.store(new FileOutputStream(root + "/myMap.txt"), null);
But I don't know how to read it.
Map<String, ArrayList<Descipline>> load = new HashMap<String, ArrayList<Descipline>>();
Properties properties1 = new Properties();
properties1.load(new FileInputStream(root + "/myMap.txt"));
for (String key : properties1.stringPropertyNames()){
//something will be here to read file
}
You're already loaded the file. Within the loop you're reading the file (i.e. the map) entries so just use again the put method
It's simple really: java.util.Properties is a subclass of java.util.HashTable, which implements java.util.Map, just like java.util.HashMap does. So you can iterate using entrySet like you do when saving, giving you both the key and value.
Note however that Properties saving and loading expects both the key and value to be String (a .properties file is a simplified 'ini' file), so you might want to look into java.io.Serializable instead.
Related
I want to store the key values pair in java from the config.properties file. Problem is it has some other which in dont want to store in array or hashmap.Below is my config.properties file. One thing the line must start with #usergroup and end of line should be End_TT_Executive as described in the file
#Usergroup
TT_Executive
#Tilename
KPI
#No of Submenu=3
#Submenu_1
OPs_KPI=https://tntanalytics3.sl1430087.sl.dst.ibm.com:8443/CAP-T/res/html/underprogress.html
#Submenu_2
Ontime_OnBudget=https://tntanalytics3.sl1430087.sl.dst.ibm.com:8443/CAP-T/res/html/underprogress.html
#submenu_3
Ops_KPI_Cloud=https://tntanalytics3.sl1430087.sl.dst.ibm.com:8443/CAP-T/res/html/underprogress.html
#Tilename
Alerting Dashboard
#No of submenu=0
Alerting_Dashboard=https://tntanalytics3.sl1430087.sl.dst.ibm.com:8443/CAP-T/res/html/underprogress.html
#Tilename
FTE_Dashboard
#No of submenu=3
#Submenu_1
FTE_Market_Sector_TT_Executive= https://tntanalytics3.sl1430087.sl.dst.ibm.com:8443/CAP-T/res/html/underprogress.html
#submenu_2
FTE_Account_TT_Executive= http://tntanalytics1.sl1430087.sl.dst.ibm.com/ibmcognos/bi/?pathRef=.public_folders%2FP=false
#Submenu_3
FTE_Laborpool_TT_Executive= https://tntanalytics3.sl1430087.sl.dst.ibm.com:8443/CAP-T/res/html/underprogress.html
#Tilename
PCR
#No of Submenu=0
PCR=https://tntanalytics3.sl1430087.sl.dst.ibm.com:8443/CAP-T/res/html/underprogress.html
End_TT_Executive
How can I do this? The key value pair are with URL only rest is some title for understanding.
Suppose your config.properties is like:
p1=abc
p2=def
p3=zxc
p4=eva
and you want to load p1 and p2 to map.
You can load all properties into Properties instance with:
InputStream inputStream = null;
try
{
inputStream = new BufferedInputStream(new FileInputStream("config.properties"));
Properties properties = new Properties();
properties.load(new InputStreamReader(inputStream, "UTF-8")); // load all properties in config.properties file
}
catch ( IOException e )
{
e.printStackTrace();
}
finally
{
inputStream.close();
}
Then you create a map:
Map<String, String> propertyMap = new HashMap<>();
You also need a String[] to store all properties which you want to load to propertyMap.
String[] wantedProperties = new String[]{"p1", "p2"};
Then you write a for loop to load the properties you wanted:
for (String property : wantedProperties) {
propertyMap.put(property, properties.getProperty(property));
}
Now propertyMap is what you want.
If you want to store to List:
List<String> propertyList = new ArrayList<>();
for (String property : wantedProperties) {
propertyList.add(properties.getProperty(property));
}
This is the way to save to list. It'll help you more if you find the solution yourself.
Is it possible to store a String ArrayList into a properties file and then read and modify the list in a simple way?
I think i will need to run example:
Properties p = new Properties();
File f = new File("MyText.txt");
FileInputStream in = new FileInputStream(f,"");
p.load(in);
ArrayList<String> list = p.getProperty("list");
<-- Modify the list and then open a OutputStream and save the p object again ?
Is this possible to manage easily in Java?
You can use a delimiter, say '|', to join the Strings in your ArrayList. When saving your list, use this code (String.join() needs Java 8):
String listAsString = String.join("|", list);
properties.put("list", listAsString);
When retrieving the list, do this:
List<String> list = Arrays.asList(properties.get("list").toString().split("\\|"));
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...
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");
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) );
}
i need to access the yaml file data into the java file.
i used the YamlReader class and now the yaml file is loaded into the java class object.
now all the information is in object and i want to extract it from this object.How can i do this .
Can any one help me please i am stuck with this problem.
You can read into a Map:
YamlReader reader = new YamlReader(new FileReader("contact.yml"));
Object object = reader.read();
System.out.println(object);
Map map = (Map)object;
System.out.println(map.get("address"));
Source: http://code.google.com/p/yamlbeans/
Yaml yaml = new Yaml();
String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae";
List<String> list = (List<String>) yaml.load(document);
System.out.println(list);
Using snakeyaml, the code below didn't work for me. When the yaml object returned my object, it returned a LinkedHashMap type. So I cast my returned object into a LinkedHashMap.
public class YamlConfig {
Yaml yaml = new Yaml();
String path = "blah blah";
InputStream input = new FileInputStream(new File(this.path));
LinkedHashMap<String,String> map;
#SuppressWarnings("unchecked")
private void loadHashMap() throws IOException {
Object data = yaml.load(input);// load the yaml document into a java object
this.map = (LinkedHashMap<String,String>)data;
System.out.println(map.entrySet()); //use this to look at your hashmap keys
System.out.println(map);//see the entire map
}
I am able to convert org.apache.commons.configuration.Configuration to java.util.Properties using:
Properties props = new Properties();
Configuration config = new PropertiesConfiguration(fileName);
Iterator iter = config.getKeys();
while (iter.hasNext()) {
String key = (String) iter.next();
String value = config.getString(key);
props.put(key, value);
}
Assumption: keys and values are of String type.
Is there any direct way to convert Configuration to Properties?
ConfigurationConverter should do the job .
It has those two methods to convert in both directions :
Configuration getConfiguration(Properties props)
Convert a standard Properties class into a configuration class
and
Properties getProperties(Configuration config)
Convert a Configuration
class into a Properties class.