问题
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
回答1:
Here is example code.
We use Scanner to parse each line of the input string. The scanner object is defined within a try-with-resources, to be automatically closed.
We define a date-time formatting pattern with DateTimeFormatter to match your funky input. Tip: Teach the publisher of your data about the ISO 8601 standard defining practical formats for exchanging date-time values as text.
As our key, we parse the first of each four lines to get a OffsetDateTime object. This is the key in our Map. Our map is defined concretely as a TreeMap, to keep the keys in sorted chronological order.
We build a List of Integer numbers parsed from the expected "a, b, c" values. We use the new List.of feature for simple syntax in building a List
object of an unknown concrete type. A List
keeps the elements in the order in which they were added, so we know the first is a
, second is b
, and third is c
.
String s = "zzz ***Wed Jan 15 10:00:03 +08 2020\n" +
"a : 20\n" +
"b : 30\n" +
"c : 40\n" +
"zzz ***Wed Jan 15 11:00:03 +08 2020\n" +
"a : 22\n" +
"b : 24\n" +
"c : 25";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "'zzz ***'EEE MMM dd HH:mm:ss x uuuu" ).withLocale( Locale.US );
Map < OffsetDateTime, List < Integer > > momentCounts = new TreeMap <>();
try (
Scanner scanner = new Scanner( s ) ;
)
{
while ( scanner.hasNextLine() )
{
OffsetDateTime moment = OffsetDateTime.parse( scanner.nextLine() , f );
List < Integer > counts = List.of(
Integer.valueOf( scanner.nextLine().replace( "a : " , "" ) ) ,
Integer.valueOf( scanner.nextLine().replace( "b : " , "" ) ) ,
Integer.valueOf( scanner.nextLine().replace( "c : " , "" ) )
);
momentCounts.put( moment , counts );
}
}
System.out.println( "momentCounts = " + momentCounts );
When run.
momentCounts = {2020-01-15T10:00:03+08:00=[20, 30, 40], 2020-01-15T11:00:03+08:00=[22, 24, 25]}
回答2:
Just init the new array before you read the new line start with "zzz".
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;
value = new ArrayList<>();
} else {
value.add(line);
}
result.put(dateString, value);
}
System.out.println(result);
来源:https://stackoverflow.com/questions/59747177/java-read-from-a-text-file-and-add-item-string-arraylist-to-hashmap