This is the text file : output1.txt
zzz ***Wed Jan 15 10:00:03 +08 2020
a : 20
b : 30
c : 40
zzz ***Wed Jan 15 11:00:03 +08 2020
a : 22
b : 24
c : 25
I am trying to add the date in String and a,b,c as ArrayList
into a hash map:
Desired output:
{zzz ***Wed Jan 15 10:00:03 +08 2020=[a : 20, b : 30, c : 40],
zzz ***Wed Jan 15 11:00:03 +08 2020=[a : 22, b : 24, c : 25]}
My code:
String dateString ="";
ArrayList<String> value = new ArrayList<String>();
HashMap<String, ArrayList> result = new HashMap<String, ArrayList>();
String fileName = "/Users/--/Downloads/output1.txt";
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("zzz")) {
dateString = line;
} else {
value.add(line);
}
result.put(dateString, value);
}
System.out.println(result);
And the result I got is:
{zzz ***Wed Jan 15 10:00:03 +08 2020=[a : 20, b : 30, c : 40, a : 22, b : 24, c : 25],
zzz ***Wed Jan 15 11:00:03 +08 2020=[a : 20, b : 30, c : 40, a : 22, b
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…