java - Want to use properties file for storing and for reading hashmap -
i have codes , corresponding states it.
i want read properties file data. not reading if populate data file. hence have supply externally using file writer. if populate data , read give error as:
file not found exception
i want data when letter in key asked. when write m, want data key starting m. eg m - mh:maharashtra,mn-manipur,ml=meghalaya,...
my code far:
public class codeproperties { public static void main(string[] args) { createcodeprop(); map<string, string> props = loadcode(); printconsole(props); } // write new properties file in java private static void createcode() { properties p = new properties(); p.setproperty("mh", "maharashtra"); p.setproperty("sk", "sikkim"); p.setproperty("mp", "madhya pradesh"); p.setproperty("mn", "manipur "); p.setproperty("ml", "meghalaya"); p.setproperty("mz", "mizoram"); filewriter writer = new filewriter("code.properties"); p.store(writer, "states , code"); writer.close(); } // read properties file in java static map<string, string> loadcode() { map<string, string> propertymap = new hashmap<string, string>(); filereader reader = new filereader("code.properties"); properties p = new properties(); p.load(reader); enumeration keys = p.propertynames(); while (keys.hasmoreelements()) { string key = (string) keys.nextelement(); propertymap.put(key, p.getproperty(key)); } reader.close(); return propertymap; } // print properties console static void printconsole(map<string, string> props) { scanner in = new scanner(system.in); system.out.println("enter code want state for"); string code = in.next(); (string key : props.keyset()) { if (key.contentequals(code)) system.out.println(key + ":" + props.get(key)); } } }
Comments
Post a Comment