我有一个具有以下格式的txt文件:
1 01/01/2018 01:00 1915 8,4
1 01/01/2018 02:00 2111 8,8
读取文件后,我要将其存储到具有以下结构的Map中:
<"Key1",1> <"Key2",01/01/2018 01:00> <"Key3",1915> <"Key4",8,4>
这是导入代码
BufferedReader buf = new BufferedReader(new
FileReader("test.txt"));
ArrayList<String> words = new ArrayList<>();
String lineJustFetched = null;
String[] wordsArray;
Map<String,String> map = new HashMap<>();
while(true){
lineJustFetched = buf.readLine();
if(lineJustFetched == null) {
break;
} else {
wordsArray = lineJustFetched.split("\t");
for(String each : wordsArray){
words.add(each);
// System.out.println(words.toString());
map.put("Key1",each);
System.out.println(map.toString());
}
}
}
buf.close();
我不知道要放入地图中具有这种结构的问题
<"Key1",1> <"Key2",01/01/2018 01:00>...
答案 0 :(得分:4)
将for
与索引一起使用
for(int i = 0 ; i < wordsArray.length ; i++) {
map.put("Key"+(i+1), wordsArray[i]);
}
在注释之后,您可以设置一个带有字段名称的数组并使用它
String[] fieldNames = {"id", "date", "whatever"};
for(int i = 0 ; i < wordsArray.length ; i++) {
map.put(fieldNames[i], wordsArray[i]);
}
答案 1 :(得分:0)
将split
的结果放置在地图中,并分配键:
wordsArray = lineJustFetched.split("\t");
map.put("Key1", wordsArray[0]);
map.put("Key2", wordsArray[1]);
map.put("Key3", wordsArray[2]);
map.put("Key4", wordsArray[3]);
答案 2 :(得分:0)
好吧,首先您的错误是您已经将100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00
硬编码为Hashmap的密钥,您应该更改它。
但是为什么不使用OOP方法呢?在正在读取的行中创建数据对象,然后将其添加到列表或其他内容中,这样,您还可以为“键”使用有意义的名称,而不仅仅是“ Key1”,“ Key2”,... >
首先,我对Java类型名/内置函数不是很熟悉,因此您可能需要更改此代码的某些部分,但是概念保持不变。如果您有任何问题,请问我:)
Key1
答案 3 :(得分:0)
希望这会达到您的目的。 我已根据您的要求更新了您的代码。输入将与您在问题中提到的相同。
BufferedReader buf = new BufferedReader(new FileReader("src/com/test/test.txt"));
ArrayList<Map> allWordsList = new ArrayList<>();
String lineJustFetched = null;
String[] wordsArray;
Map<String, String> map;
String[] fieldNames = { "Id", "Date", "Code", "No" };
while ((lineJustFetched = buf.readLine()) != null) {
wordsArray = lineJustFetched.split("\\s+");
map = new HashMap<>();
for (int i = 0, j = i; i < wordsArray.length; i++) {
if (i == 1) map.put(fieldNames[j++], wordsArray[i] + " " + wordsArray[i + 1]);
if (i != 1 && i != 2) map.put(fieldNames[j++], wordsArray[i]);
}
allWordsList.add(map);
}
buf.close();
System.out.println(allWordsList);
您将得到的输出为
[{No=8,4, Id=1, Code=1915, Date=01/01/2018 01:00},
{No=8,8, Id=1, Code=2111, Date=01/01/2018 02:00}]