This is the text file:
#Person
PRIM_KEY=personId
SEX=gender
YEARS=age
NAME=fullName
#Automobil
PRIM_KEY=registrationNumber
MAKE=manufacturer
TYPE=model
Read the file:
Scanner scanner = new Scanner(new FileReader("C:/workspace/column-mapping.txt"));
When I encounter #Person
, in the following map defined:
Map<String, String> personMap = new LinkedHashMap<String, String>();
I want to store the key-value pairs below it.
So store these key-value pairs in personMap
:
PRIM_KEY=personId
SEX=gender
YEARS=age
NAME=fullName
Similarly when I encounter #Automobil
, in the following map
Map<String, String> automobilMap = new LinkedHashMap<String, String>();
I want these key-value pairs stored:
PRIM_KEY=registrationNumber
MAKE=manufacturer
TYPE=model
When I read the file, how to store these key-value pairs in two different maps depending upon, in this example, #Person
and #Automobil
?
EDIT Sample Code:
Scanner scanner = new Scanner(new FileReader("C:/workspace/column-mapping.txt"));
Map<String, String> personMap = new LinkedHashMap<String, String>();
Map<String, String> automobilMap = new LinkedHashMap<String, String>();
String line;
while (scanner.hasNext()) {
line = scanner.next();
if (!line.startsWith("#") && !line.isEmpty()) {
String[] columns = line.split("=");
personMap.put(columns[0], columns[1]);
}
}
System.out.println(personMap);
This way I can put all key-value pairs in one map personMap
. But depending upon sections, I want to be able to put it in different maps.