java - read an unformatted text file and store its value in hashmap -


i have text file containing data in below format

  vehicle:bike   model:fz   make:   yamaha   description   abcdefgh   ijklmn   problems   gear problem, fork bend.    auto data   ***********************************end***********************   vehicle:bike   model:r15   make:   yamaha   description   1234,   567.   890   problems   gear problem, fork bend.   oil leakage    auto data   ***********************************end*********************** 

i have given 2 datas there many more such in text file want read , store in hashmap such that

  bike:fz:yamaha:abcdefghijklmn:gear problem,fork bend.   bike:r15:yamaha:1234,567.890:gear problem,fork bend.oil leakage 

my sample code:

public static void main(string[] args) {          try {             bufferedreader br = new bufferedreader(new filereader("data.txt"));              string scurrentline;             int = 0;             int j = 0;             hmap = new hashmap<string, integer>();             while ((scurrentline = br.readline()) != null) {                  system.out.println(scurrentline);                 scurrentline = scurrentline.trim();                 if (!scurrentline.equals("")) // don't write out blank lines                 {                     if (scurrentline.startswith("***********")) {                         i++;                     } else {                          if (scurrentline.startswith("vehicle:")) {                             string[] veh = scurrentline.split(":");                             string vehicle = ttype[1];                         }                         if (scurrentline.startswith("model:")) {                             string[] mod = scurrentline.split(":");                             string model = ishield[1];                         }                           hmap.put(0,i+":"+vehicle+":"+model);                                           }                 } j++;             }         } catch (ioexception e) {             e.printstacktrace();         }      } 

not sure how read ---> make, description & problems attributes.

you'll need objectinputstream.

an example:

/* create objectinputstream text file  * , hash map store values in. */ objectinputstream obj = new objectinputstream(new fileinputstream(textfile)); hmap = (hashmap<string, string>) obj.readobject(); // assume want strings. hmap.put("value", var); // var can whatever other strings created. // idea close streams. obj.close(); 

just remember that, if need variable type placed hash map, can create hashmap<string, byte[]>.

obviously, you'll need implement already-created methods determine each variable.

if have not been specific enough, or have missed important, let me know.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -