java 读取文本文件并将键值对存储在不同的映射中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31695019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read the text file and store the key-value pairs in different maps
提问by SASM
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:
当我遇到#Person
,在下面的地图中定义:
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
:
我想在它下面存储键值对。因此,将这些键值对存储在personMap
:
PRIM_KEY=personId
SEX=gender
YEARS=age
NAME=fullName
Similarly when I encounter #Automobil
, in the following map
同样的,当我遇到时#Automobil
,在下图中
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
?
当我读文件,如何存储在两个不同的地图,这些键值对,这取决于,在这个例子中,#Person
和#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.
这样我就可以将所有键值对放在一个 map 中personMap
。但是根据不同的部分,我希望能够将它放在不同的地图中。
回答by Niles
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.
我想向您推荐两种不同的方法。第一种方法只是用 if 案例解决您的问题,在第二种方法中,我假设您想要存储多个 Automobil 和多个 Person,因此我创建了两个类来解决问题。
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;
}
}
}
回答by SASM
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);