gpt4 book ai didi

java - 如何使用 Java 读取属性文件中的组级别值?

转载 作者:行者123 更新时间:2023-12-01 09:14:48 26 4
gpt4 key购买 nike

属性文件示例:

Label1=DataSet1
Label1.Col1=val1
Label1.Col2=val2
Label1.Col3=val3
Label1.Col4=val4

Label2=DataSet2
Label2.Col1=val1
Label2.Col2=val2
Label2.Col3=val3
Label2.Col4=val4

当我想读取DataSet 1 & 2数据时。我应该得到所有相应的键和值。我怎样才能用 Java 编写这段代码?你能帮我一下吗?

数据集1:

Col1=val1
Col2=val2...

最佳答案

试试这个:

ReadGroupLevel.txt:

Label1=DataSet1
Label1.Col1=val1
Label1.Col2=val2
Label1.Col3=val3
Label1.Col4=val4

Label2=DataSet2
Label2.Col1=val1
Label2.Col2=val2
Label2.Col3=val3
Label2.Col4=val4

Label3=DataSet3
Label3.Col1=val1
Label3.Col4=val4

Label4=DataSet4
Label4.Col1=val1
Label4.Col2=val2
Label4.Col3=val3
Label4.Col4=val4

代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;



public class readGroupLevel {

static Map<Integer,String> dataset = new HashMap<Integer, String>();

public static void main(String[] args) throws IOException {
// 1 - Read the file and sore it in "dataset"
readFile();

// 2 - Get content of dataset
System.out.println("Dataset 1:");
System.out.println(dataset.get(1));

System.out.println("Dataset 2:");
System.out.println(dataset.get(2));

System.out.println("Dataset 3:");
System.out.println(dataset.get(3));

System.out.println("Dataset 4:");
System.out.println(dataset.get(4));

System.out.println("Dataset 5:");
System.out.println(dataset.get(5));

}

/**
* Reads the file and store it in dataset
* @throws IOException
*/
private static void readFile() throws IOException{

// Read the file
FileReader in = new FileReader("src/ReadGroupLevel.txt");
BufferedReader br = new BufferedReader(in);

// Current line
String line;

// Number of dataset find
int numberDataset = -1;

// Content of current dataset
String contentDataset="";

while ((line = br.readLine()) != null) {

// Find line "Label1=DataSet1"
if(line.contains("DataSet")){

if(numberDataset!=-1){
// Fill data
dataset.put(numberDataset, contentDataset);
contentDataset = "";
numberDataset=-1;
}

// Split line "Label1=DataSet1" by "=" and get number
String[] split = line.split("=");
String nameDataset=split[1];
try{
numberDataset = Integer.parseInt(nameDataset.replace("DataSet", ""));
}catch(Exception e){
numberDataset = -1;
}
}else if(numberDataset!=-1){
// Get content
contentDataset = contentDataset+line+"\n";
}
}

// Last dataset content
if(numberDataset!=-1){
dataset.put(numberDataset, contentDataset);
}

in.close();
}

}

输出:

Dataset 1:
Label1.Col1=val1
Label1.Col2=val2
Label1.Col3=val3
Label1.Col4=val4


Dataset 2:
Label2.Col1=val1
Label2.Col2=val2
Label2.Col3=val3
Label2.Col4=val4


Dataset 3:
Label3.Col1=val1
Label3.Col4=val4


Dataset 4:
Label4.Col1=val1
Label4.Col2=val2
Label4.Col3=val3
Label4.Col4=val4

Dataset 5:
null

关于java - 如何使用 Java 读取属性文件中的组级别值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40640267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com