简体   繁体   English

如何读取文件,以便对于包含特定字符串的每一行,将下面的行保存到 HashMap?

[英]How do I read in a file such that for each line that contains a certain string, save the lines underneath to a HashMap?

I admit this might be a simple question but I cannot seem to map my head around it.我承认这可能是一个简单的问题,但我似乎无法思考 map。 Say that I have a.txt file that I'm reading with a BufferedReader in Java that might look like this:假设我有一个 .txt 文件,我正在使用 Java 中的 BufferedReader 读取该文件,该文件可能如下所示:

Category: Dairy
Yogurt
Cheese
Butter
Category: Vegetables
Carrots
Cucumbers
Category: Snacks
Granola Bars
Trail Mix
Chips
Candy
Pop Tarts

I want to create a HashMap, for example, called groceries that will map the items to its respective category.例如,我想创建一个 HashMap,称为杂货,将 map 项目归入其各自的类别。 For example:例如:

groceries.put("item", "category");

How do I read this file in such a way that keeps track of which item belongs to which category so I can place it into the HashMap in a meaningful way?如何以跟踪哪个项目属于哪个类别的方式读取此文件,以便以有意义的方式将其放入 HashMap 中? Here is a rough idea of what I have tried so far.这是我迄今为止尝试过的大致想法。 (I tried initially with ArrayLists but figured out HashMaps might be better to map the data) (我最初尝试使用 ArrayLists 但发现 HashMaps 可能比 map 数据更好)

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            if (line.contains("Category")){
                
                  (save in an arrayList() called Category)

            }
            else{
                  (save in arrayList() called Item)
            }
        }

Again that is just what I had originally tried.同样,这正是我最初尝试过的。 Let me know if any further clarification is needed.让我知道是否需要进一步澄清。

This can inspire (before anyone closes it ): The structure you want to build is a map (key-value structure) not a list.这可以启发(在任何人关闭它之前):您要构建的结构是 map(键值结构)而不是列表。

BufferedReader in = new BufferedReader(new InputStreamReader(System.in)));
String line;
String currentCategory = "";
Map<String, String> groceries = new HashMap<>();
while ((line = in.readLine()) != null) {
    if (line.startsWith("Category: ")) {
        currentCategory = line.substring(10);
    } else {
        groceries.put(line, currentCategory);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:读取文本文件时,如何读取包含特定字符串的特定行? - Java: When reading a text file, how can i read that specific line which contains a certain string? 如何从txt文件读取行并将每行添加到LinkedList类型? - How do I read lines from a txt file and add each line to a type LinkedList? 如何保存包含类的 Hashmap? - How do I save a Hashmap which contains a class? 如何使用扫描仪从文件中读取一行中的某个字符串? - How do I read a certain string on a line from a file, using scanner? 如何将文件的每一行保存到数组? - How do i save each line of the file to an Array? 缓冲阅读器:使用 HashMap 读取并保存一行的某些部分(在 Java 中?) - Buffered Reader: read and save certain parts of a line with HashMap (in java?) 如何在 Java 中使用 Scanner 读取多行并在某个字符串之前结束? - How do I read multiple lines and end before a certain string using Scanner in Java? 将哈希图保存并读取到文件? - Save and read hashmap to file? 如何逐行读取文件并每行追加一些字符串? - how to read a file line by line and append some string each line? 如何将文本文件读入arraylist,但不包括包含某些单词的行? - How do I read a text file into an arraylist, excluding lines that contain certain words?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM