简体   繁体   中英

Read the text file and store the key-value pairs in different maps

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.

I want to propose you two different approaches. The first approach is simply solving your problem with a an if case, in the second approach I am supposing you want to store multiple Automobil and multiple Person so I am creating two classes to solve the problem.

I hope this can be useful, it was interesting to make this code

First approach

public static void main(String args[]) throws FileNotFoundException{
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= scanner.nextLine();

    while(scanner.hasNext()){

        Map<String, String> currentMap = null;
        if(line.equals("#Person")){
            currentMap=personMap;
        }
        if(line.equals("#Automobil")){
            currentMap=automobilMap;
        }
        while(scanner.hasNext()){
            line=scanner.nextLine();
            if(line.startsWith("#"))
                break; //restart the loop
            String splitted[] = line.split("=");
            currentMap.put(splitted[0], splitted[1]);
        }            
    }

}

Second approach

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new FileReader("C:/workspace/column-mapping.txt"));
    Map<String, Person> personMap = new LinkedHashMap<String, Person>();
    Map<String, Car> automobilMap = new LinkedHashMap<String, Car>();
    String line;

    while (scanner.hasNextLine()) {
        line = scanner.nextLine();
        if(line.equals("#Person")){
            String primaryKey = scanner.nextLine().split("=")[1];
            String sex = scanner.nextLine().split("=")[1];
            String years = scanner.nextLine().split("=")[1];
            String name = scanner.nextLine().split("=")[1];
            personMap.put(primaryKey, new Person(sex,years,name));
        }
        if(line.equals("#Automobil")){
            String primaryKey = scanner.nextLine().split("=")[1];
            String make = scanner.nextLine().split("=")[1];
            String type = scanner.nextLine().split("=")[1];
            automobilMap.put(primaryKey, new Car(make,type));
        }

    }

    Set<String> personKeys = personMap.keySet();
    Set<String> automobilKeys = automobilMap.keySet();

    for(String k : personKeys){
        System.out.println("Person: "+k);
        System.out.println(personMap.get(k));
    }

    for(String k : automobilKeys){
        System.out.println("Car: "+k);
        System.out.println(automobilMap.get(k));
    }
}

The two classes for the second approach

public class Person {
boolean male; //true for male, false for female
int years;
String name;
String surname;

public Person(String gender, String age, String fullName){
    if(gender.equals("male"))
        male = true;
    else
        male = false;
    years=Integer.parseInt(age);
    String splitted[] = fullName.split(" ");
    name = splitted[0];
    surname = splitted[1];
}

@Override
public String toString(){
    String gender = "male";
    if(!male)
        gender = "female";
    return "SEX = "+gender+"\nYEARS = "+years+"\nNAME = "+name+" "+surname;
}
}

public class Car {
String make;
String type;

public Car(String manufacturer,String model){
    make=manufacturer;
    type=model;
}

@Override
public String toString(){
    return "MAKE = "+make+"\nTYPE = "+type;
}

}

Not the most elegant solution, but got it working.

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;
boolean person = false;
boolean automobil= false;

while (scanner.hasNext()) {
     line = scanner.next();
     if(line.startsWith("#Person") && !line.isEmpty()){
        line = scanner.next();
        person = true;
        automobil = false;
     }

     if(line.startsWith("#Automobil") && !line.isEmpty()){
        line = scanner.next();
        automobil = true;
        person = false;
     }

     if(person){
        if (!line.startsWith("#") && !line.isEmpty()) {
            String[] columns = line.split("=");
            for(String str:columns){
                 System.out.println(str);
            }
            personMap.put(columns[0], columns[1]);
        }
     }

     if(automobil){
         if (!line.startsWith("#") && !line.isEmpty()) {
             String[] columns = line.split("=");
             for(String str:columns){
                  System.out.println(str);
             }
             automobilMap.put(columns[0], columns[1]);
         }
      }



}
System.out.println("personMap"+personMap);
System.out.println("automobilMap"+automobilMap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM