java - Read textfile with repeating entries into list (similar to properties) -
is there proper way read textfile this
picture = germany correctanswer = germany otherletters = klbcdop picture = italy correctanswer = italy otherletters = ouilmnkds picture = egypt correctanswer = egypt otherletters = ywmpsadch picture = usa correctanswer = usa otherletters = klpomnagsyw
and store data in - example - list? access data calling
string picture = list.get(0).get("picture"); string otherletters = list.get(0).get("otherletters"); ...
sure, manually, there not better way this?
as far concern question, can create arraylist of hashmap below.. replace d:\file_to_read.txt file path..
try { file file = new file("d:\\file_to_read.txt"); filereader filereader = new filereader(file); bufferedreader bufferedreader = new bufferedreader(filereader); string line; hashmap<string, string> map = new hashmap<string, string>(); string arry[] = null; arraylist<hashmap<string, string>> list = new arraylist<hashmap<string, string>>(); while ((line = bufferedreader.readline()) != null) { if ("".equals(line)) { list.add(map); map = new hashmap<string, string>(); system.out.println("blank"); } else { arry = line.split("="); map.put(arry[0].trim(), arry[1].trim()); } } string picture = list.get(0).get("picture"); string otherletters = list.get(0).get("otherletters"); filereader.close(); } catch (ioexception e) { e.printstacktrace(); } }
Comments
Post a Comment