java - Writing and reading ListMultimap<Object, Object> in file -
i tried writing listmultimap
file using properties
, seems impossible, refer question writing , reading listmultimap file using properties.
going ahead, if using properties
store listmultimap
not correct way, how can store listmultimap
file? , how can read file?
e.g. lets have:
listmultimap<object, object> index = arraylistmultimap.create();
how can write methods write listmultimap
file , read file:
writetofile(listmultimap multimap, string filepath){ //?? } listmultimap readfromfile(string filepath){ listmultimap multimap; //multimap = read file return multimap; }
you need decide how represent each object in file. example, if listmultimap
contained string
s write string value if you're dealing complex objects need produce representation of object byte[]
, if want use properties
should base64 encoded.
the basic read method should like:
public listmultimap<object, object> read(inputstream in) throws ioexception { listmultimap<object, object> index = arraylistmultimap.create(); properties properties = new properties(); properties.load(in); (object serializedkey : properties.keyset()) { string deserializedkey = deserialize(serializedkey); string values = properties.get(serializedkey); (string value : values.split(",")) { index.put(deserializedkey, deserialize(value)); } } return index; }
and write method this:
public void write(listmultimap<object, object> index, outputstream out) throws ioexception { properties properties = new properties(); (object key : index.keyset()) { stringbuilder values = new stringbuilder(); (object value = index.get(key)) { values.append(serailize(value)).append(","); } properties.setproperty(serailize(key), values.substring(0, values.length - 1)); } properties.store(out, "saving"); }
this example makes use of serialize
, deserialize
methods you'll need define according requirements signatures are:
public string serialize(object object)
and
public object deserialize(string s)
Comments
Post a Comment